Skip to content

feat: add Huawei auth adapter #7721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Adapters/Auth/huawei.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"use strict";

const https = require('https');
var Parse = require('parse/node').Parse;

const TOKEN_ISSUER = "https://accounts.huawei.com";
const HUAWEI_URL = "https://oauth-login.cloud.huawei.com/oauth2/v3/tokeninfo";


function getDataFromHuawei(id_token){
return new Promise((resolve, reject) => {
https.get(`${HUAWEI_URL}?id_token=${id_token}`, res => {
let data = '';
res.on('data', chunk => {
data += chunk.toString('utf8');
});
res.on('end', () => {
resolve(JSON.parse(data));
});
}).on('error', reject);
});
}


async function verifyIdToken({ id_token: token, id }) {
if (!token) {
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, `id token is invalid for this user.`);
}
const huaweiData = await getDataFromHuawei(token);

if(!huaweiData){
throw new Parse.Error(Parse.Error.OTHER_CAUSE, `Undefined error from Huawei API.`);
}
else if(huaweiData.error){
throw new Parse.Error(Parse.Error.OTHER_CAUSE,
`${huaweiData.error_description} (${huaweiData.error}::${huaweiData.sub_error})`
);
} // Error from huawei
const { sub, iss, exp, iat } = huaweiData;

// Content Check
if(iss !== TOKEN_ISSUER)
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'ID token not issued by correct provider.');
if(id !== sub)
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Auth data is invalid for this user.');

// Expiration Check
if(exp < Date.now() / 1000)
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'ID token is expired.');
if(iat > Date.now() / 1000)
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'ID token not issued by correct provider or method.');
} // Returns a promise that fulfills if this user id is valid.


function validateAuthData(authData, options = {}) {
return verifyIdToken(authData, options);
}

function validateAppId() {
return Promise.resolve();
}

module.exports = {
validateAppId: validateAppId,
validateAuthData: validateAuthData
};
2 changes: 2 additions & 0 deletions src/Adapters/Auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const phantauth = require('./phantauth');
const microsoft = require('./microsoft');
const keycloak = require('./keycloak');
const ldap = require('./ldap');
const huawei = require('./huawei')

const anonymous = {
validateAuthData: () => {
Expand Down Expand Up @@ -59,6 +60,7 @@ const providers = {
microsoft,
keycloak,
ldap,
huawei
};

function authDataValidator(adapter, appIds, options) {
Expand Down