WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c3a2a32

Browse files
committed
fix: errors
1 parent 03e5dbd commit c3a2a32

File tree

8 files changed

+248
-127
lines changed

8 files changed

+248
-127
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"serialize-error": "^8.1.0",
9999
"serialize-javascript": "^6.0.0",
100100
"tough-cookie": "^4.0.0",
101-
"undici": "^5.0.0",
101+
"undici": "^5.8.0",
102102
"uuid": "^8.3.2",
103103
"yargs": "^17.4.0",
104104
"zxcvbn": "^4.4.2"

pnpm-lock.yaml

Lines changed: 206 additions & 114 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/actions/oauth/upgrade.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const appleStrategy = require('../../auth/oauth/strategies/apple');
2020
* @apiParam (Payload) {String} isStatelessAuth - use stateless JWT tokens when they are optional
2121
*/
2222
async function upgrade(request) {
23-
const { transportRequest, params, query } = request;
23+
const { transportRequest, params, query, log } = request;
2424
const { provider, token, isStatelessAuth } = params;
2525

2626
// fetch settings, otherwise provider is not supported
@@ -39,10 +39,13 @@ async function upgrade(request) {
3939

4040
const credentials = provider === 'apple'
4141
? await appleStrategy.upgradeAppleCode({
42-
query,
43-
providerSettings,
44-
code: token,
45-
redirectUrl: transportRequest.url.href.replace(/\/upgrade$/, '/apple').replace(/^http:\/\//, 'https://'),
42+
log,
43+
params: {
44+
query,
45+
providerSettings,
46+
code: token,
47+
redirectUrl: transportRequest.url.href.replace(/\/upgrade$/, '/apple').replace(/^http:\/\//, 'https://'),
48+
},
4649
})
4750
: await profile.call(providerSettings, { token, query });
4851

src/auth/oauth/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ async function mserviceVerification({ service, transportRequest }, credentials,
111111

112112
// user is authenticated and profile is attached
113113
if (user && userId) {
114+
// @TODO code?
114115
throw new Errors.HttpStatusError(412, 'profile is linked');
115116
}
116117

src/auth/oauth/strategies/apple.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ const Bluebird = require('bluebird');
44
const Boom = require('@hapi/boom');
55
const { request: httpRequest } = require('undici');
66

7+
const {
8+
ERROR_OAUTH_APPLE_VALIDATE_CODE,
9+
ERROR_OAUTH_APPLE_VERIFY_PROFILE,
10+
} = require('../../../constants');
11+
712
// @todo more options from config
813
const jwksClient = getJwksClient({
914
jwksUri: 'https://appleid.apple.com/auth/keys',
@@ -151,13 +156,24 @@ function getProvider(options, server) {
151156
};
152157
}
153158

154-
async function upgradeAppleCode(params) {
159+
async function upgradeAppleCode({ params, log }) {
155160
const { providerSettings, code, query, redirectUrl } = params;
156161
const { profile } = providerSettings.provider;
157162

163+
let tokenResponse;
164+
165+
try {
166+
tokenResponse = await validateGrantCode(providerSettings, code, redirectUrl);
167+
} catch (error) {
168+
log.error(Boom.internal(error.body?.error, undefined, error.statusCode));
169+
170+
throw ERROR_OAUTH_APPLE_VALIDATE_CODE;
171+
}
172+
173+
let credentials;
174+
158175
try {
159-
const tokenResponse = await validateGrantCode(providerSettings, code, redirectUrl);
160-
const credentials = await profile.call(
176+
credentials = await profile.call(
161177
providerSettings,
162178
{
163179
query,
@@ -167,11 +183,13 @@ async function upgradeAppleCode(params) {
167183
},
168184
tokenResponse
169185
);
170-
171-
return credentials;
172186
} catch (error) {
173-
throw Boom.internal(error.body?.error, undefined, error.statusCode);
187+
log.error(Boom.internal(error.body?.error, undefined, error.statusCode));
188+
189+
throw ERROR_OAUTH_APPLE_VERIFY_PROFILE;
174190
}
191+
192+
return credentials;
175193
}
176194

177195
module.exports = {

src/auth/oauth/utils/form-oauth-response.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ async function formOAuthResponse(ctx, request, credentials) {
3434
}
3535

3636
if (!account) {
37+
// @TODO 403?
3738
throw new HttpStatusError(500, 'no account when jwt isn\'t present');
3839
}
3940

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ module.exports = exports = {
8989
ErrorUserNotMember: new HttpStatusError(404, 'username not member of organization'),
9090
ErrorInvitationExpiredOrUsed: new HttpStatusError(400, 'Invitation has expired or already been used'),
9191

92+
ERROR_OAUTH_APPLE_VALIDATE_CODE: new HttpStatusError(403, 'Code validation failed'),
93+
ERROR_OAUTH_APPLE_VERIFY_PROFILE: new HttpStatusError(403, 'Profile verification failed'),
94+
9295
// actions
9396
USERS_ACTION_ACTIVATE: 'activate',
9497
USERS_ACTION_VERIFY_CONTACT: 'verify-contact',
@@ -143,6 +146,9 @@ exports.USERS_JWT_ACCESS_REQUIRED.code = 'E_TKN_ACCESS_TOKEN_REQUIRED';
143146
exports.USERS_JWT_REFRESH_REQUIRED.code = 'E_TKN_REFRESH_TOKEN_REQUIRED';
144147
exports.USERS_JWT_STATELESS_REQUIRED.code = 'E_STATELESS_NOT_ENABLED';
145148

149+
exports.ERROR_OAUTH_APPLE_VALIDATE_CODE.code = 'E_VALIDATE_CODE';
150+
exports.ERROR_OAUTH_APPLE_VERIFY_PROFILE.code = 'E_VERIFY_PROFILE';
151+
146152
exports.SSO_PROVIDERS = [
147153
exports.USERS_SSO_FACEBOOK_FIELD,
148154
exports.USERS_SSO_APPLE_FIELD,

test/suites/actions/oauth/upgrade.apple.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('oauth.upgrade action', function suite() {
2828
appleStrategyStub
2929
.withArgs(
3030
match(
31-
(params) => params.code === 'c75da8efcf25f4acb80e51152fead9fad.0.srqty.7-k4X-G9bBesI_9hDFH6Xg'
31+
({ params }) => params.code === 'c75da8efcf25f4acb80e51152fead9fad.0.srqty.7-k4X-G9bBesI_9hDFH6Xg'
3232
&& params.redirectUrl === 'https://ms-users.local/users/oauth/apple'
3333
&& params.providerSettings.appId === 'com.test.app'
3434
)

0 commit comments

Comments
 (0)