Skip to content

Commit

Permalink
Merge pull request #168 from ringcentral/jwt-auth
Browse files Browse the repository at this point in the history
Adding support for JWT auth
  • Loading branch information
byrnereese authored Jan 28, 2022
2 parents f9b328c + 27aba59 commit eb71da9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
36 changes: 36 additions & 0 deletions sdk/src/platform/Platform-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ describe('RingCentral.platform.Platform', () => {
}),
);

it(
'login with JWT',
asyncTest(async sdk => {
const platform = sdk.platform();

await platform.auth().cancelAccessToken();

authentication();

await platform.login({
jwt: 'foo',
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
}),
);

it(
'login with username/password',
asyncTest(async sdk => {
const platform = sdk.platform();

await platform.auth().cancelAccessToken();

authentication();

await platform.login({
username: 'foo',
password: 'foo',
extension: 'foo',
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
}),
);

it(
'login with code from usePKCE flow without client secret',
asyncTest(
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/platform/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ export default class Platform extends EventEmitter {
password,
extension = '',
code,
jwt,
access_token_ttl,
refresh_token_ttl,
access_token,
Expand Down Expand Up @@ -452,14 +453,17 @@ export default class Platform extends EventEmitter {
//TODO Potentially make a request to /oauth/tokeninfo
json = {access_token, ...options};
} else {
if (!code) {
if (!code && !jwt) {
body.grant_type = 'password';
if (extension && extension.length > 0) {
body.username = `${username}*${extension}`;
} else {
body.username = username;
}
body.password = password;
} else if (jwt) {
body.grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer';
body.assertion = jwt;
} else if (code) {
//@see https://developers.ringcentral.com/legacy-api-reference/index.html#!#RefAuthorizationCodeFlow
body.grant_type = 'authorization_code';
Expand Down Expand Up @@ -834,6 +838,7 @@ export interface LoginOptions {
password?: string;
extension?: string;
code?: string;
jwt?: string;
access_token?: string;
access_token_ttl?: number;
refresh_token_ttl?: number;
Expand Down

0 comments on commit eb71da9

Please sign in to comment.