0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -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);
});
});
describe('POST /session/sign-in/social/auth', () => {
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',
state: 'state',
redirectUri: 'https://logto.dev',
@ -379,7 +381,7 @@ describe('sessionRoutes', () => {
});
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',
state: 'state',
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 () => {
const response = await sessionRequest.post('/session/sign-in/social').send({
const response = await sessionRequest.post('/session/sign-in/social/auth').send({
connectorId: 'connectorId',
state: 'state',
redirectUri: 'https://logto.dev',
@ -411,7 +413,7 @@ describe('sessionRoutes', () => {
});
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_',
state: 'state',
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 () => {
await expect(
sessionRequest
.post('/session/sign-in/bind-social-related-user')
.post('/session/bind-social-related-user')
.send({ connectorId: 'connectorId' })
).resolves.toHaveProperty('statusCode', 400);
});
@ -441,7 +443,7 @@ describe('sessionRoutes', () => {
});
await expect(
sessionRequest
.post('/session/sign-in/bind-social-related-user')
.post('/session/bind-social-related-user')
.send({ connectorId: 'connectorId' })
).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',
});
expect(response.statusCode).toEqual(200);

View file

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

File diff suppressed because it is too large Load diff