-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(authentication): bitbucket-provider (#398)
- Loading branch information
1 parent
f815666
commit d093b88
Showing
6 changed files
with
120 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
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, | ||
}, | ||
}, | ||
}; |
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
8 changes: 8 additions & 0 deletions
8
modules/authentication/src/handlers/oauth2/bitbucket/bitbucket.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
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
69
modules/authentication/src/handlers/oauth2/bitbucket/bitbucket.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,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', | ||
}, | ||
}; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
modules/authentication/src/handlers/oauth2/bitbucket/bitbucket.user.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,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; | ||
} |
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