-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from pilcrowOnPaper/amazon-cognito-provider
Add Amazon Cognito provider
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: "Amazon Cognito" | ||
--- | ||
|
||
# Amazon Cognito | ||
|
||
Implements OpenID Connect. | ||
|
||
For usage, see [OAuth 2.0 provider with PKCE](/guides/oauth2-pkce). | ||
|
||
```ts | ||
import { AmazonCognito } from "arctic"; | ||
|
||
const userPoolDomain = "https://example.auth.region.amazoncognito.com"; | ||
const amazonCognito = new AmazonCognito(userPoolDomain, clientId, clientSecret, redirectURI); | ||
``` | ||
|
||
```ts | ||
const url: URL = await amazonCognito.createAuthorizationURL(state, codeVerifier, { | ||
// optional | ||
scopes // "openid" always included | ||
}); | ||
const tokens: AmazonCognitoTokens = await amazonCognito.validateAuthorizationCode( | ||
code, | ||
codeVerifier | ||
); | ||
const tokens: AmazonCognitoRefreshedTokens = await amazonCognito.refreshAccessToken(refreshToken); | ||
``` | ||
|
||
## Get user profile | ||
|
||
Parse the ID token or use the `userinfo` endpoint. See [sample ID token claims](https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims). | ||
|
||
```ts | ||
const tokens = await amazonCognito.validateAuthorizationCode(code, codeVerifier); | ||
const response = await fetch(userPoolDomain + "/oauth/userInfo", { | ||
headers: { | ||
Authorization: `Bearer ${tokens.accessToken}` | ||
} | ||
}); | ||
const user = await response.json(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { TimeSpan, createDate } from "oslo"; | ||
import { OAuth2Client } from "oslo/oauth2"; | ||
|
||
import type { OAuth2ProviderWithPKCE } from "../index.js"; | ||
|
||
export class AmazonCognito implements OAuth2ProviderWithPKCE { | ||
private client: OAuth2Client; | ||
private clientSecret: string; | ||
|
||
constructor(userPoolDomain: string, clientId: string, clientSecret: string, redirectURI: string) { | ||
const authorizeEndpoint = userPoolDomain + "/oauth2/authorize"; | ||
const tokenEndpoint = userPoolDomain + "/oauth2/token"; | ||
this.client = new OAuth2Client(clientId, authorizeEndpoint, tokenEndpoint, { | ||
redirectURI | ||
}); | ||
this.clientSecret = clientSecret; | ||
} | ||
|
||
public async createAuthorizationURL( | ||
state: string, | ||
codeVerifier: string, | ||
options?: { | ||
scopes?: string[]; | ||
} | ||
): Promise<URL> { | ||
const scopes = options?.scopes ?? []; | ||
return await this.client.createAuthorizationURL({ | ||
state, | ||
codeVerifier, | ||
scopes: [...scopes, "openid"] | ||
}); | ||
} | ||
|
||
public async validateAuthorizationCode( | ||
code: string, | ||
codeVerifier: string | ||
): Promise<AmazonCognitoTokens> { | ||
const result = await this.client.validateAuthorizationCode<AuthorizationCodeResponseBody>( | ||
code, | ||
{ | ||
credentials: this.clientSecret, | ||
codeVerifier | ||
} | ||
); | ||
const tokens: AmazonCognitoTokens = { | ||
accessToken: result.access_token, | ||
refreshToken: result.refresh_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")), | ||
idToken: result.id_token | ||
}; | ||
return tokens; | ||
} | ||
|
||
public async refreshAccessToken(refreshToken: string): Promise<AmazonCognitoRefreshedTokens> { | ||
const result = await this.client.refreshAccessToken<RefreshTokenResponseBody>(refreshToken, { | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: AmazonCognitoRefreshedTokens = { | ||
accessToken: result.access_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")), | ||
idToken: result.id_token | ||
}; | ||
return tokens; | ||
} | ||
} | ||
|
||
interface AuthorizationCodeResponseBody { | ||
access_token: string; | ||
refresh_token: string; | ||
expires_in: number; | ||
id_token: string; | ||
} | ||
|
||
interface RefreshTokenResponseBody { | ||
access_token: string; | ||
expires_in: number; | ||
id_token: string; | ||
} | ||
|
||
export interface AmazonCognitoTokens { | ||
accessToken: string; | ||
refreshToken: string; | ||
accessTokenExpiresAt: Date; | ||
idToken: string; | ||
} | ||
|
||
export interface AmazonCognitoRefreshedTokens { | ||
accessToken: string; | ||
accessTokenExpiresAt: Date; | ||
idToken: string; | ||
} |