From 60f8a7ccdc9c53e5caeda34d946b560340a41cf5 Mon Sep 17 00:00:00 2001 From: andr35 <9112303+andr35@users.noreply.github.com> Date: Sun, 29 Oct 2023 04:41:14 +0100 Subject: [PATCH] Add "serverUrl" param to GitlabAuth config (#1230) --- .auri/$avmnsr2u.md | 6 ++++++ .../content/oauth/providers/gitlab.md | 16 +++++++++------- packages/oauth/src/providers/gitlab.ts | 19 ++++++++++++++----- 3 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 .auri/$avmnsr2u.md diff --git a/.auri/$avmnsr2u.md b/.auri/$avmnsr2u.md new file mode 100644 index 000000000..cfa1bba70 --- /dev/null +++ b/.auri/$avmnsr2u.md @@ -0,0 +1,6 @@ +--- +package: "@lucia-auth/oauth" +type: "minor" +--- + +Add `serverUrl` param to `GitlabAuth` config diff --git a/documentation/content/oauth/providers/gitlab.md b/documentation/content/oauth/providers/gitlab.md index eb31fdf7f..1d4639559 100644 --- a/documentation/content/oauth/providers/gitlab.md +++ b/documentation/content/oauth/providers/gitlab.md @@ -24,19 +24,21 @@ const gitlab: ( clientSecret: string; redirectUri: string; scope?: string[]; + serverUrl?: string; } ) => GitlabProvider; ``` ##### Parameters -| name | type | description | optional | -| --------------------- | ------------------------------------------ | ------------------------------ | :------: | -| `auth` | [`Auth`](/reference/lucia/interfaces/auth) | Lucia instance | | -| `config.clientId` | `string` | GitLab OAuth app client id | | -| `config.clientSecret` | `string` | GitLab OAuth app client secret | | -| `config.redirectUri` | `string` | an authorized redirect URI | | -| `config.scope` | `string[]` | an array of scopes | ✓ | +| name | type | description | optional | +| --------------------- | ------------------------------------------ | --------------------------------------------- | :------: | +| `auth` | [`Auth`](/reference/lucia/interfaces/auth) | Lucia instance | | +| `config.clientId` | `string` | GitLab OAuth app client id | | +| `config.clientSecret` | `string` | GitLab OAuth app client secret | | +| `config.redirectUri` | `string` | an authorized redirect URI | | +| `config.scope` | `string[]` | an array of scopes | ✓ | +| `config.serverUrl` | `string` | URL of GitLab, to use a self-managed instance | ✓ | ##### Returns diff --git a/packages/oauth/src/providers/gitlab.ts b/packages/oauth/src/providers/gitlab.ts index e526586a3..334646c97 100644 --- a/packages/oauth/src/providers/gitlab.ts +++ b/packages/oauth/src/providers/gitlab.ts @@ -13,6 +13,7 @@ type Config = { clientSecret: string; redirectUri: string; scope?: string[]; + serverUrl?: string; }; const PROVIDER_ID = "gitlab"; @@ -28,11 +29,13 @@ export class GitlabAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth< GitlabUserAuth<_Auth> > { private config: Config; + private readonly serverUrl: string; constructor(auth: _Auth, config: Config) { super(auth); this.config = config; + this.serverUrl = config.serverUrl || "https://gitlab.com"; } public getAuthorizationUrl = async (): Promise< @@ -40,7 +43,7 @@ export class GitlabAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth< > => { const scopeConfig = this.config.scope ?? []; return await createOAuth2AuthorizationUrl( - "https://gitlab.com/oauth/authorize", + `${this.serverUrl}/oauth/authorize`, { clientId: this.config.clientId, redirectUri: this.config.redirectUri, @@ -53,7 +56,10 @@ export class GitlabAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth< code: string ): Promise> => { const gitlabTokens = await this.validateAuthorizationCode(code); - const gitlabUser = await getGitlabUser(gitlabTokens.accessToken); + const gitlabUser = await getGitlabUser( + gitlabTokens.accessToken, + this.serverUrl + ); return new GitlabUserAuth(this.auth, gitlabUser, gitlabTokens); }; @@ -64,7 +70,7 @@ export class GitlabAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth< access_token: string; expires_in: number; refresh_token: string; - }>(code, "https://gitlab.com/oauth/token", { + }>(code, `${this.serverUrl}/oauth/token`, { clientId: this.config.clientId, redirectUri: this.config.redirectUri, clientPassword: { @@ -94,8 +100,11 @@ export class GitlabUserAuth< } } -const getGitlabUser = async (accessToken: string): Promise => { - const request = new Request("https://gitlab.com/api/v4/user", { +const getGitlabUser = async ( + accessToken: string, + serverUrl: string +): Promise => { + const request = new Request(`${serverUrl}/api/v4/user`, { headers: { Authorization: authorizationHeader("bearer", accessToken) }