Skip to content

Commit

Permalink
feat: grantTokenByRefreshToken (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangsijie authored Oct 15, 2021
1 parent eddb25e commit 34f0081
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
25 changes: 24 additions & 1 deletion packages/client/src/grant-token.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';

import { grantTokenByAuthorizationCode } from './grant-token';
import { grantTokenByAuthorizationCode, grantTokenByRefreshToken } from './grant-token';

describe('grantTokenByAuthorizationCode', () => {
test('get tokenSet paramaters', async () => {
Expand Down Expand Up @@ -46,3 +46,26 @@ describe('grantTokenByAuthorizationCode', () => {
).rejects.toThrowError();
});
});

describe('grantTokenByRefreshToken', () => {
test('get tokenSet paramaters', async () => {
const successResponse = {
access_token: 'access_token',
expires_in: 3600,
id_token: 'id_token',
refresh_token: 'refresh_token',
};

nock('https://logto.dev', { allowUnmocked: true })
.post('/oidc/token')
.reply(200, successResponse);

const tokenSet = await grantTokenByRefreshToken(
'https://logto.dev/oidc/token',
'client_id',
'refresh_token'
);

expect(tokenSet).toMatchObject(successResponse);
});
});
24 changes: 24 additions & 0 deletions packages/client/src/grant-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ export const grantTokenByAuthorizationCode = async (

return result.data;
};

export const grantTokenByRefreshToken = async (
endpoint: string,
clientId: string,
refreshToken: string
): Promise<TokenSetParameters> => {
const parameters = new URLSearchParams();
parameters.append('grant_type', 'refresh_token');
parameters.append('client_id', clientId);
parameters.append('refresh_token', refreshToken);

const response = await opRequest.post(endpoint, parameters, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
const result = TokenSetParametersSchema.safeParse(response.data);

if (!result.success) {
throw new OPError({ originalError: result.error });
}

return result.data;
};

0 comments on commit 34f0081

Please sign in to comment.