Skip to content

Commit

Permalink
Add "serverUrl" param to GitlabAuth config (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
andr35 authored Oct 29, 2023
1 parent aba44f9 commit 60f8a7c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .auri/$avmnsr2u.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
package: "@lucia-auth/oauth"
type: "minor"
---

Add `serverUrl` param to `GitlabAuth` config
16 changes: 9 additions & 7 deletions documentation/content/oauth/providers/gitlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 14 additions & 5 deletions packages/oauth/src/providers/gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config = {
clientSecret: string;
redirectUri: string;
scope?: string[];
serverUrl?: string;
};

const PROVIDER_ID = "gitlab";
Expand All @@ -28,19 +29,21 @@ 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<
readonly [url: URL, state: string]
> => {
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,
Expand All @@ -53,7 +56,10 @@ export class GitlabAuth<_Auth extends Auth = Auth> extends OAuth2ProviderAuth<
code: string
): Promise<GitlabUserAuth<_Auth>> => {
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);
};

Expand All @@ -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: {
Expand Down Expand Up @@ -94,8 +100,11 @@ export class GitlabUserAuth<
}
}

const getGitlabUser = async (accessToken: string): Promise<GitlabUser> => {
const request = new Request("https://gitlab.com/api/v4/user", {
const getGitlabUser = async (
accessToken: string,
serverUrl: string
): Promise<GitlabUser> => {
const request = new Request(`${serverUrl}/api/v4/user`, {
headers: {
Authorization: authorizationHeader("bearer", accessToken)
}
Expand Down

0 comments on commit 60f8a7c

Please sign in to comment.