-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
rltjqdl1138
wants to merge
10
commits into
parse-community:alpha
Choose a base branch
from
rltjqdl1138:alpha
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e15e99e
Add huawei auth adapter
rltjqdl1138 64206ec
Fix: Error message and etc for PR
rltjqdl1138 fd6e7ac
Merge branch 'alpha' into alpha
mtrezza 19d9a26
fix:update huawei error messages
rltjqdl1138 4ca02ae
Merge branch 'parse-community:alpha' into alpha
rltjqdl1138 80896ad
fix: remove usless space from eslint
rltjqdl1138 99cd312
fix: update error messages
rltjqdl1138 5888ade
fix: update error message
rltjqdl1138 a0524cf
Merge branch 'parse-community:alpha' into alpha
rltjqdl1138 6ccb356
Merge branch 'alpha' into alpha
mtrezza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.