Skip to content

Commit

Permalink
modified: src/providers/linkedin.ts (#105)
Browse files Browse the repository at this point in the history
* modified:   src/providers/linkedin.ts

* add id_token to LinkedIn

* add id_token to LinkedIn

* add id_token to LinkedIn

* add changesets

* Update .changesets/a6vql.minor.md

* Update .changesets/dj9ch.patch.md

---------

Co-authored-by: pilcrow <pilcrowonpaper@gmail.com>
  • Loading branch information
gongfan99 and pilcrowonpaper authored Apr 27, 2024
1 parent 09d5f50 commit adf70ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions .changesets/a6vql.minor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Feat: Add `idToken` to the return value of LinkedIn's `validateAuthorizationCode(code: string)`
1 change: 1 addition & 0 deletions .changesets/dj9ch.patch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix: Make `refreshToken` optional for the return value of LinkedIn's `validateAuthorizationCode(code: string)`
42 changes: 32 additions & 10 deletions src/providers/linkedin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,31 @@ export class LinkedIn implements OAuth2Provider {
}

public async validateAuthorizationCode(code: string): Promise<LinkedInTokens> {
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, {
authenticateWith: "request_body",
credentials: this.clientSecret
});
const result = await this.client.validateAuthorizationCode<AuthorizationCodeResponseBody>(
code,
{
authenticateWith: "request_body",
credentials: this.clientSecret
}
);
const tokens: LinkedInTokens = {
idToken: result.id_token,
accessToken: result.access_token,
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")),
refreshToken: result.refresh_token,
refreshTokenExpiresAt: createDate(new TimeSpan(result.refresh_token_expires_in, "s"))
refreshToken: result.refresh_token ?? null,
refreshTokenExpiresAt: result.refresh_token_expires_in
? createDate(new TimeSpan(result.refresh_token_expires_in, "s"))
: null
};
return tokens;
}

public async refreshAccessToken(accessToken: string): Promise<LinkedInTokens> {
const result = await this.client.refreshAccessToken<TokenResponseBody>(accessToken, {
public async refreshAccessToken(accessToken: string): Promise<LinkedInRefreshedTokens> {
const result = await this.client.refreshAccessToken<RefreshTokenResponseBody>(accessToken, {
authenticateWith: "request_body",
credentials: this.clientSecret
});
const tokens: LinkedInTokens = {
const tokens: LinkedInRefreshedTokens = {
accessToken: result.access_token,
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")),
refreshToken: result.refresh_token,
Expand All @@ -59,14 +65,30 @@ export class LinkedIn implements OAuth2Provider {
}
}

interface TokenResponseBody {
interface AuthorizationCodeResponseBody {
id_token: string;
access_token: string;
expires_in: number;
refresh_token?: string; // available only if your application is authorized for programmatic refresh tokens
refresh_token_expires_in?: number;
}

interface RefreshTokenResponseBody {
access_token: string;
expires_in: number;
refresh_token: string;
refresh_token_expires_in: number;
}

export interface LinkedInTokens {
idToken: string;
accessToken: string;
accessTokenExpiresAt: Date;
refreshToken: string | null;
refreshTokenExpiresAt: Date | null;
}

export interface LinkedInRefreshedTokens {
accessToken: string;
accessTokenExpiresAt: Date;
refreshToken: string;
Expand Down

0 comments on commit adf70ca

Please sign in to comment.