123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- "use strict";
- const Parse = require('parse/node').Parse;
- const querystring = require('querystring');
- const httpsRequest = require('./httpsRequest');
- const INVALID_ACCESS = 'OAuth2 access token is invalid for this user.';
- const INVALID_ACCESS_APPID = "OAuth2: the access_token's appID is empty or is not in the list of permitted appIDs in the auth configuration.";
- const MISSING_APPIDS = 'OAuth2 configuration is missing the client app IDs ("appIds" config parameter).';
- const MISSING_URL = 'OAuth2 token introspection endpoint URL is missing from configuration!';
- function validateAuthData(authData, options) {
- return requestTokenInfo(options, authData.access_token).then(response => {
- if (!response || !response.active || options.useridField && authData.id !== response[options.useridField]) {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, INVALID_ACCESS);
- }
- });
- }
- function validateAppId(appIds, authData, options) {
- if (!options || !options.appidField) {
- return Promise.resolve();
- }
- if (!appIds || appIds.length === 0) {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, MISSING_APPIDS);
- }
- return requestTokenInfo(options, authData.access_token).then(response => {
- if (!response || !response.active) {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, INVALID_ACCESS);
- }
- const appidField = options.appidField;
- if (!response[appidField]) {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, INVALID_ACCESS_APPID);
- }
- const responseValue = response[appidField];
- if (!Array.isArray(responseValue) && appIds.includes(responseValue)) {
- return;
- } else if (Array.isArray(responseValue) && responseValue.some(appId => appIds.includes(appId))) {
- return;
- } else {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, INVALID_ACCESS_APPID);
- }
- });
- }
- function requestTokenInfo(options, access_token) {
- if (!options || !options.tokenIntrospectionEndpointUrl) {
- throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, MISSING_URL);
- }
- const parsedUrl = new URL(options.tokenIntrospectionEndpointUrl);
- const postData = querystring.stringify({
- token: access_token
- });
- const headers = {
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Content-Length': Buffer.byteLength(postData)
- };
- if (options.authorizationHeader) {
- headers['Authorization'] = options.authorizationHeader;
- }
- const postOptions = {
- hostname: parsedUrl.hostname,
- path: parsedUrl.pathname,
- method: 'POST',
- headers: headers
- };
- return httpsRequest.request(postOptions, postData);
- }
- module.exports = {
- validateAppId: validateAppId,
- validateAuthData: validateAuthData
- };
|