0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

feat(core): separate social sign-in api (#735)

* feat(core): separate social sign-in api

* feat(core): rename APIs
This commit is contained in:
Darcy Ye 2022-05-07 10:50:37 +08:00 committed by GitHub
parent 5db628b716
commit e71cf7ea67
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 406 additions and 391 deletions

View file

@ -367,9 +367,11 @@ describe('sessionRoutes', () => {
}); });
expect(response.statusCode).toEqual(404); expect(response.statusCode).toEqual(404);
}); });
});
describe('POST /session/sign-in/social/auth', () => {
it('throw error when auth code is wrong', async () => { it('throw error when auth code is wrong', async () => {
const response = await sessionRequest.post('/session/sign-in/social').send({ const response = await sessionRequest.post('/session/sign-in/social/auth').send({
connectorId: 'connectorId', connectorId: 'connectorId',
state: 'state', state: 'state',
redirectUri: 'https://logto.dev', redirectUri: 'https://logto.dev',
@ -379,7 +381,7 @@ describe('sessionRoutes', () => {
}); });
it('throw error when code is provided but connector can not be found', async () => { it('throw error when code is provided but connector can not be found', async () => {
const response = await sessionRequest.post('/session/sign-in/social').send({ const response = await sessionRequest.post('/session/sign-in/social/auth').send({
connectorId: '_connectorId', connectorId: '_connectorId',
state: 'state', state: 'state',
redirectUri: 'https://logto.dev', redirectUri: 'https://logto.dev',
@ -389,7 +391,7 @@ describe('sessionRoutes', () => {
}); });
it('get and add user info with auth code, as well as assign result and redirect', async () => { it('get and add user info with auth code, as well as assign result and redirect', async () => {
const response = await sessionRequest.post('/session/sign-in/social').send({ const response = await sessionRequest.post('/session/sign-in/social/auth').send({
connectorId: 'connectorId', connectorId: 'connectorId',
state: 'state', state: 'state',
redirectUri: 'https://logto.dev', redirectUri: 'https://logto.dev',
@ -411,7 +413,7 @@ describe('sessionRoutes', () => {
}); });
it('throw error when identity exists', async () => { it('throw error when identity exists', async () => {
const response = await sessionRequest.post('/session/sign-in/social').send({ const response = await sessionRequest.post('/session/sign-in/social/auth').send({
connectorId: '_connectorId_', connectorId: '_connectorId_',
state: 'state', state: 'state',
redirectUri: 'https://logto.dev', redirectUri: 'https://logto.dev',
@ -427,11 +429,11 @@ describe('sessionRoutes', () => {
}); });
}); });
describe('POST /session/sign-in/bind-social-related-user', () => { describe('POST /session/bind-social-related-user', () => {
it('throw if session is not authorized', async () => { it('throw if session is not authorized', async () => {
await expect( await expect(
sessionRequest sessionRequest
.post('/session/sign-in/bind-social-related-user') .post('/session/bind-social-related-user')
.send({ connectorId: 'connectorId' }) .send({ connectorId: 'connectorId' })
).resolves.toHaveProperty('statusCode', 400); ).resolves.toHaveProperty('statusCode', 400);
}); });
@ -441,7 +443,7 @@ describe('sessionRoutes', () => {
}); });
await expect( await expect(
sessionRequest sessionRequest
.post('/session/sign-in/bind-social-related-user') .post('/session/bind-social-related-user')
.send({ connectorId: 'connectorId' }) .send({ connectorId: 'connectorId' })
).resolves.toHaveProperty('statusCode', 400); ).resolves.toHaveProperty('statusCode', 400);
}); });
@ -455,7 +457,7 @@ describe('sessionRoutes', () => {
}, },
}, },
}); });
const response = await sessionRequest.post('/session/sign-in/bind-social-related-user').send({ const response = await sessionRequest.post('/session/bind-social-related-user').send({
connectorId: 'connectorId', connectorId: 'connectorId',
}); });
expect(response.statusCode).toEqual(200); expect(response.statusCode).toEqual(200);

View file

@ -190,26 +190,39 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
koaGuard({ koaGuard({
body: object({ body: object({
connectorId: string(), connectorId: string(),
code: string().optional(),
state: string(), state: string(),
redirectUri: string().regex(redirectUriRegEx), redirectUri: string().regex(redirectUriRegEx),
}), }),
}), }),
async (ctx, next) => { async (ctx, next) => {
const { connectorId, code, state, redirectUri } = ctx.guard.body; const { connectorId, state, redirectUri } = ctx.guard.body;
const type = 'SignInSocial'; const type = 'SignInSocial';
ctx.log(type, { connectorId, code, state, redirectUri }); ctx.log(type, { connectorId, state, redirectUri });
if (!code) { assertThat(state && redirectUri, 'session.insufficient_info');
assertThat(state && redirectUri, 'session.insufficient_info'); const connector = await getSocialConnectorInstanceById(connectorId);
const connector = await getSocialConnectorInstanceById(connectorId); assertThat(connector.connector.enabled, 'connector.not_enabled');
assertThat(connector.connector.enabled, 'connector.not_enabled'); const redirectTo = await connector.getAuthorizationUri(redirectUri, state);
const redirectTo = await connector.getAuthorizationUri(redirectUri, state); ctx.body = { redirectTo };
ctx.body = { redirectTo }; ctx.log(type, { redirectTo });
ctx.log(type, { redirectTo });
return next(); return next();
} }
);
router.post(
'/session/sign-in/social/auth',
koaGuard({
body: object({
connectorId: string(),
code: string(),
redirectUri: string().regex(redirectUriRegEx),
}),
}),
async (ctx, next) => {
const { connectorId, code, redirectUri } = ctx.guard.body;
const type = 'SignInSocial';
ctx.log(type, { connectorId, code, redirectUri });
const userInfo = await getUserInfoByAuthCode(connectorId, code, redirectUri); const userInfo = await getUserInfoByAuthCode(connectorId, code, redirectUri);
ctx.log(type, { userInfo }); ctx.log(type, { userInfo });
@ -241,7 +254,7 @@ export default function sessionRoutes<T extends AnonymousRouter>(router: T, prov
); );
router.post( router.post(
'/session/sign-in/bind-social-related-user', '/session/bind-social-related-user',
koaGuard({ koaGuard({
body: object({ connectorId: string() }), body: object({ connectorId: string() }),
}), }),

740
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff