-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add authentik provider * add changesets * Remove tokenType Co-authored-by: pilcrow <pilcrowonpaper@gmail.com> * Update authentik docs Co-authored-by: pilcrow <pilcrowonpaper@gmail.com> * Update authentik docs Co-authored-by: pilcrow <pilcrowonpaper@gmail.com> * Update src/providers/authentik.ts Co-authored-by: pilcrow <pilcrowonpaper@gmail.com> * removing token type and instance realmURL * Updating the code block to be consistent with other providers * Update authentik docs * Update authentik.md * Update authentik.ts * Update doh0b.minor.md * Update authentik.md --------- Co-authored-by: pilcrow <pilcrowonpaper@gmail.com>
- Loading branch information
1 parent
e17432a
commit 674b9ef
Showing
8 changed files
with
176 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added Authentik auth provider |
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 |
---|---|---|
|
@@ -2,4 +2,5 @@ pnpm-lock.yaml | |
dist | ||
node_modules | ||
.DS_Store | ||
package-lock.json | ||
package-lock.json | ||
.history/ |
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,48 @@ | ||
--- | ||
title: "Authentik" | ||
--- | ||
|
||
# Authentik | ||
|
||
For usage, see [OAuth 2.0 provider with PKCE](/guides/oauth2-pkce). | ||
|
||
```ts | ||
import { Authentik } from "arctic"; | ||
|
||
const realmURL = "http://example.com"; | ||
const authentik = new Authentik(realmURL, clientId, clientSecret, redirectURI); | ||
``` | ||
|
||
```ts | ||
const url: URL = await authentik.createAuthorizationURL(state, codeVerifier, { | ||
// optional | ||
scopes // "openid" always included | ||
}); | ||
const tokens: AuthentikTokens = await authentik.validateAuthorizationCode(code, codeVerifier); | ||
const tokens: AuthentikTokens = await authentik.refreshAccessToken(refreshToken); | ||
``` | ||
|
||
## Get refresh token | ||
|
||
Authentik with version 2024.2 and higher only provides the access token by default. To get the refresh token as well, you'll need to include the `offline_access` scope. The scope also needs to be enabled in your app's advanced settings (Application > Providers > Edit > Advanced protocol settings > Scopes). | ||
|
||
|
||
```ts | ||
const url: URL = await authentik.createAuthorizationURL(state, codeVerifier, { | ||
scopes: ["profile", "email", "offline_access"] | ||
}); | ||
``` | ||
|
||
## Get user profile | ||
|
||
Authentik provides endpoint `/application/o/userinfo/` that you can use to fetch the user info once you obtain the Bearer token. | ||
|
||
```ts | ||
const tokens = await authentik.validateAuthorizationCode(code, codeVerifier); | ||
const response = await fetch("https://example.com/application/o/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,76 @@ | ||
import { OAuth2Client } from "oslo/oauth2"; | ||
import { TimeSpan, createDate } from "oslo"; | ||
import type { OAuth2ProviderWithPKCE } from "../index.js"; | ||
|
||
export class Authentik implements OAuth2ProviderWithPKCE { | ||
private client: OAuth2Client; | ||
private clientSecret: string; | ||
|
||
constructor(realmURL: string, clientId: string, clientSecret: string, redirectURI: string) { | ||
const authorizeEndpoint = realmURL + "/application/o/authorize/"; | ||
const tokenEndpoint = realmURL + "/application/o/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<AuthentikTokens> { | ||
const result = await this.client.validateAuthorizationCode<TokenResponseBody>(code, { | ||
codeVerifier, | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: AuthentikTokens = { | ||
accessToken: result.access_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")), | ||
refreshToken: result.refresh_token ?? null, | ||
idToken: result.id_token | ||
}; | ||
return tokens; | ||
} | ||
|
||
public async refreshAccessToken(refreshToken: string): Promise<AuthentikTokens> { | ||
const result = await this.client.refreshAccessToken<TokenResponseBody>(refreshToken, { | ||
credentials: this.clientSecret | ||
}); | ||
const tokens: AuthentikTokens = { | ||
accessToken: result.access_token, | ||
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")), | ||
refreshToken: result.refresh_token ?? null, | ||
idToken: result.id_token | ||
}; | ||
return tokens; | ||
} | ||
} | ||
|
||
interface TokenResponseBody { | ||
access_token: string; | ||
expires_in: number; | ||
refresh_token?: string; | ||
id_token: string; | ||
} | ||
|
||
export interface AuthentikTokens { | ||
accessToken: string; | ||
accessTokenExpiresAt: Date; | ||
refreshToken: string | null; | ||
idToken: string; | ||
} |
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