Skip to content

Commit

Permalink
feat(authentication): bitbucket-provider (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
SotiriaSte authored Oct 21, 2022
1 parent f815666 commit d093b88
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
25 changes: 25 additions & 0 deletions modules/authentication/src/config/bitbucket.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
bitbucket: {
enabled: {
format: 'Boolean',
default: false,
},
clientId: {
format: 'String',
default: '',
},
clientSecret: {
format: 'String',
default: '',
},
redirect_uri: {
format: 'String',
default: '',
},
accountLinking: {
doc: 'When enabled, if a new bitbucket user matches with an existing email on the database, they will be enriched with bitbucket details',
format: 'Boolean',
default: true,
},
},
};
3 changes: 3 additions & 0 deletions modules/authentication/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import tokenConfig from './token.config';
import localConfig from './local.config';
import magicLinkConfig from './magicLink.config';
import gitlabConfig from './gitlab.config';
import bitbucketConfig from './bitbucket.config';
import linkedInConfig from './linkedIn.config';


const AppConfigSchema = {
...DefaultConfig,
...figmaConfig,
Expand All @@ -26,6 +28,7 @@ const AppConfigSchema = {
...localConfig,
...magicLinkConfig,
...gitlabConfig,
...bitbucketConfig,
...linkedInConfig,
};
const config = convict(AppConfigSchema);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"accessTokenMethod": "POST",
"authorizeUrl": "https://bitbucket.org/site/oauth2/authorize?",
"providerName": "bitbucket",
"tokenUrl": "https://bitbucket.org/site/oauth2/access_token",
"responseType": "code",
"grantType": "authorization_code"
}
69 changes: 69 additions & 0 deletions modules/authentication/src/handlers/oauth2/bitbucket/bitbucket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk';
import axios from 'axios';
import { BitbucketUser } from './bitbucket.user';
import { OAuth2 } from '../OAuth2';
import { OAuth2Settings } from '../interfaces/OAuth2Settings';
import * as bitbucketParameters from './bitbucket.json';
import { ProviderConfig } from '../interfaces/ProviderConfig';
import { Payload } from '../interfaces/Payload';
import { ConnectionParams } from '../interfaces/ConnectionParams';
import { isNil } from 'lodash';
import { AuthParams } from '../interfaces/AuthParams';

export class BitbucketHandlers extends OAuth2<BitbucketUser, OAuth2Settings> {
constructor(grpcSdk: ConduitGrpcSdk, config: { bitbucket: ProviderConfig }) {
super(
grpcSdk,
'bitbucket',
new OAuth2Settings(config.bitbucket, bitbucketParameters),
);
this.defaultScopes = ['account'];
}

async connectWithProvider(details: ConnectionParams): Promise<Payload<BitbucketUser>> {
const bitbucket_access_token = details.accessToken;
const bitbucketProfile = await axios.get('https://api.bitbucket.org/2.0/user', {
headers: {
Authorization: `Bearer ${bitbucket_access_token}`,
Accept: 'application/json',
},
});

let email;
if (isNil(bitbucketProfile.data.email)) {
const bitbucketUserEmail = await axios.get(
'https://api.bitbucket.org/2.0/user/emails',
{
headers: {
Authorization: `Bearer ${bitbucket_access_token}`,
Accept: 'application/json',
},
},
);
email = bitbucketUserEmail.data.values[0].email;
} else {
email = bitbucketProfile.data.email;
}
return {
id: bitbucketProfile.data.uuid,
email: email,
data: { ...bitbucketProfile.data },
};
}
makeRequest(data: AuthParams) {
const requestData: string = Object.keys(data)
.map(k => {
return k + '=' + data[k as keyof AuthParams];
})
.join('&');

return {
method: this.settings.accessTokenMethod,
url: this.settings.tokenUrl,
data: requestData,
headers: {
Accept: 'application/json',
},
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface BitbucketUser {
display_name: string;
created_on?: string;
type: string;
uuid: string;
has_2fa_enabled: boolean;
username: string;
is_staff: boolean;
account_id: string;
nickname: string;
account_status: string;
location?: string;
website?: string;
}
1 change: 1 addition & 0 deletions modules/authentication/src/handlers/oauth2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export * from './microsoft/microsoft';
export * from './slack/slack';
export * from './twitch/twitch';
export * from './gitlab/gitlab';
export * from './bitbucket/bitbucket';
export * from './linkedIn/linkedin';

0 comments on commit d093b88

Please sign in to comment.