-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[server] add basic support for BitBucket Server
- Loading branch information
1 parent
30da763
commit 7c2fd47
Showing
12 changed files
with
624 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
104 changes: 104 additions & 0 deletions
104
components/server/src/bitbucket-server/bitbucket-server-api.ts
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,104 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import fetch from 'node-fetch'; | ||
import { User } from "@gitpod/gitpod-protocol"; | ||
import { inject, injectable } from "inversify"; | ||
import { AuthProviderParams } from "../auth/auth-provider"; | ||
import { BitbucketTokenHelper } from "../bitbucket/bitbucket-token-handler"; | ||
|
||
@injectable() | ||
export class BitbucketServerApi { | ||
|
||
@inject(AuthProviderParams) protected readonly config: AuthProviderParams; | ||
@inject(BitbucketTokenHelper) protected readonly tokenHelper: BitbucketTokenHelper; | ||
|
||
public async runQuery<T>(user: User, urlPath: string): Promise<T> { | ||
const token = (await this.tokenHelper.getTokenWithScopes(user, [])).value; | ||
const fullUrl = `${this.baseUrl}${urlPath}`; | ||
const response = await fetch(fullUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'Authorization': `Bearer ${token}` | ||
} | ||
}); | ||
if (!response.ok) { | ||
throw Error(response.statusText); | ||
} | ||
const result = await response.json(); | ||
return result as T; | ||
} | ||
|
||
protected get baseUrl(): string { | ||
return `https://${this.config.host}/rest/api/1.0`; | ||
} | ||
|
||
getRepository(user: User, params: { kind: "projects" | "users", userOrProject: string; repositorySlug: string; }): Promise<BitbucketServer.Repository> { | ||
return this.runQuery<BitbucketServer.Repository>(user, `/${params.kind}/${params.userOrProject}/repos/${params.repositorySlug}`); | ||
} | ||
|
||
getCommits(user: User, params: { kind: "projects" | "users", userOrProject: string, repositorySlug: string, q?: { limit: number } }): Promise<BitbucketServer.Paginated<BitbucketServer.Commit>> { | ||
return this.runQuery<BitbucketServer.Paginated<BitbucketServer.Commit>>(user, `/${params.kind}/${params.userOrProject}/repos/${params.repositorySlug}/commits`); | ||
} | ||
} | ||
|
||
|
||
export namespace BitbucketServer { | ||
export interface Repository { | ||
id: number; | ||
slug: string; | ||
name: string; | ||
public: boolean; | ||
links: { | ||
clone: { | ||
href: string; | ||
name: string; | ||
}[] | ||
} | ||
project: Project; | ||
} | ||
|
||
export interface Project { | ||
key: string; | ||
id: number; | ||
name: string; | ||
public: boolean; | ||
} | ||
|
||
export interface User { | ||
"name": string, | ||
"emailAddress": string, | ||
"id": number, | ||
"displayName": string, | ||
"active": boolean, | ||
"slug": string, | ||
"type": string, | ||
"links": { | ||
"self": [ | ||
{ | ||
"href": string | ||
} | ||
] | ||
} | ||
} | ||
|
||
export interface Commit { | ||
"id": string, | ||
"displayId": string, | ||
"author": BitbucketServer.User | ||
} | ||
|
||
export interface Paginated<T> { | ||
isLastPage?: boolean; | ||
limit?: number; | ||
size?: number; | ||
start?: number; | ||
values?: T[]; | ||
[k: string]: any; | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
components/server/src/bitbucket-server/bitbucket-server-auth-provider.ts
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,111 @@ | ||
/** | ||
* Copyright (c) 2022 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { AuthProviderInfo } from "@gitpod/gitpod-protocol"; | ||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import * as express from "express"; | ||
import { injectable } from "inversify"; | ||
import fetch from "node-fetch"; | ||
import { AuthUserSetup } from "../auth/auth-provider"; | ||
import { GenericAuthProvider } from "../auth/generic-auth-provider"; | ||
import { BitbucketServerOAuthScopes } from "./bitbucket-server-oauth-scopes"; | ||
import * as BitbucketServer from "@atlassian/bitbucket-server"; | ||
|
||
@injectable() | ||
export class BitbucketServerAuthProvider extends GenericAuthProvider { | ||
|
||
get info(): AuthProviderInfo { | ||
return { | ||
...this.defaultInfo(), | ||
scopes: BitbucketServerOAuthScopes.ALL, | ||
requirements: { | ||
default: BitbucketServerOAuthScopes.Requirements.DEFAULT, | ||
publicRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT, | ||
privateRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT, | ||
}, | ||
} | ||
} | ||
|
||
/** | ||
* Augmented OAuthConfig for Bitbucket | ||
*/ | ||
protected get oauthConfig() { | ||
const oauth = this.params.oauth!; | ||
const scopeSeparator = " "; | ||
return <typeof oauth>{ | ||
...oauth, | ||
authorizationUrl: oauth.authorizationUrl || `https://${this.params.host}/rest/oauth2/latest/authorize`, | ||
tokenUrl: oauth.tokenUrl || `https://${this.params.host}/rest/oauth2/latest/token`, | ||
settingsUrl: oauth.settingsUrl || `https://${this.params.host}/plugins/servlet/oauth/users/access-tokens/`, | ||
scope: BitbucketServerOAuthScopes.ALL.join(scopeSeparator), | ||
scopeSeparator | ||
}; | ||
} | ||
|
||
protected get tokenUsername(): string { | ||
return "x-token-auth"; | ||
} | ||
|
||
authorize(req: express.Request, res: express.Response, next: express.NextFunction, scope?: string[]): void { | ||
super.authorize(req, res, next, scope ? scope : BitbucketServerOAuthScopes.Requirements.DEFAULT); | ||
} | ||
|
||
protected readAuthUserSetup = async (accessToken: string, _tokenResponse: object) => { | ||
try { | ||
const fetchResult = await fetch(`https://${this.params.host}/plugins/servlet/applinks/whoami`, { | ||
headers: { | ||
"Authorization": `Bearer ${accessToken}`, | ||
} | ||
}); | ||
if (!fetchResult.ok) { | ||
throw new Error(fetchResult.statusText); | ||
} | ||
const username = await fetchResult.text(); | ||
if (!username) { | ||
throw new Error("username missing"); | ||
} | ||
|
||
log.warn(`(${this.strategyName}) username ${username}`); | ||
|
||
const options = { | ||
baseUrl: `https://${this.params.host}`, | ||
}; | ||
const client = new BitbucketServer(options); | ||
|
||
client.authenticate({ type: "token", token: accessToken }); | ||
const result = await client.api.getUser({ userSlug: username }); | ||
|
||
const user = result.data; | ||
|
||
// TODO: check if user.active === true? | ||
|
||
return <AuthUserSetup>{ | ||
authUser: { | ||
authId: `${user.id!}`, | ||
authName: user.slug!, | ||
primaryEmail: user.emailAddress!, | ||
name: user.displayName!, | ||
// avatarUrl: user.links!.avatar!.href // TODO | ||
}, | ||
currentScopes: BitbucketServerOAuthScopes.ALL, | ||
} | ||
|
||
} catch (error) { | ||
log.error(`(${this.strategyName}) Reading current user info failed`, error, { accessToken, error }); | ||
throw error; | ||
} | ||
} | ||
|
||
protected normalizeScopes(scopes: string[]) { | ||
const set = new Set(scopes); | ||
for (const item of set.values()) { | ||
if (!(BitbucketServerOAuthScopes.Requirements.DEFAULT.includes(item))) { | ||
set.delete(item); | ||
} | ||
} | ||
return Array.from(set).sort(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
components/server/src/bitbucket-server/bitbucket-server-container-module.ts
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,37 @@ | ||
/** | ||
* Copyright (c) 2020 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License-AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { ContainerModule } from "inversify"; | ||
import { AuthProvider } from "../auth/auth-provider"; | ||
import { BitbucketApiFactory } from "../bitbucket/bitbucket-api-factory"; | ||
import { BitbucketFileProvider } from "../bitbucket/bitbucket-file-provider"; | ||
import { BitbucketLanguagesProvider } from "../bitbucket/bitbucket-language-provider"; | ||
import { BitbucketRepositoryProvider } from "../bitbucket/bitbucket-repository-provider"; | ||
import { BitbucketTokenHelper } from "../bitbucket/bitbucket-token-handler"; | ||
import { FileProvider, LanguagesProvider, RepositoryHost, RepositoryProvider } from "../repohost"; | ||
import { IContextParser } from "../workspace/context-parser"; | ||
import { BitbucketServerApi } from "./bitbucket-server-api"; | ||
import { BitbucketServerAuthProvider } from "./bitbucket-server-auth-provider"; | ||
import { BitbucketServerContextParser } from "./bitbucket-server-context-parser"; | ||
|
||
export const bitbucketServerContainerModule = new ContainerModule((bind, _unbind, _isBound, _rebind) => { | ||
bind(RepositoryHost).toSelf().inSingletonScope(); | ||
bind(BitbucketServerApi).toSelf().inSingletonScope(); | ||
bind(BitbucketFileProvider).toSelf().inSingletonScope(); | ||
bind(FileProvider).toService(BitbucketFileProvider); | ||
bind(BitbucketServerContextParser).toSelf().inSingletonScope(); | ||
bind(BitbucketLanguagesProvider).toSelf().inSingletonScope(); | ||
bind(LanguagesProvider).toService(BitbucketLanguagesProvider); | ||
bind(IContextParser).toService(BitbucketServerContextParser); | ||
bind(BitbucketRepositoryProvider).toSelf().inSingletonScope(); | ||
bind(RepositoryProvider).toService(BitbucketRepositoryProvider); | ||
bind(BitbucketServerAuthProvider).toSelf().inSingletonScope(); | ||
bind(AuthProvider).to(BitbucketServerAuthProvider).inSingletonScope(); | ||
bind(BitbucketTokenHelper).toSelf().inSingletonScope(); | ||
bind(BitbucketApiFactory).toSelf().inSingletonScope(); | ||
// bind(BitbucketTokenValidator).toSelf().inSingletonScope(); // TODO | ||
// bind(IGitTokenValidator).toService(BitbucketTokenValidator); | ||
}); |
Oops, something went wrong.