Skip to content

Commit 93c9fa8

Browse files
[AXON-46] Add remote auth flow using atlascode-backend
1 parent b148985 commit 93c9fa8

File tree

12 files changed

+408
-232
lines changed

12 files changed

+408
-232
lines changed

src/atlclients/authInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export enum OAuthProvider {
4141
BitbucketCloudStaging = 'bbcloudstaging',
4242
JiraCloud = 'jiracloud',
4343
JiraCloudStaging = 'jiracloudstaging',
44+
JiraCloudRemote = 'jiracloudremote',
4445
}
4546
export interface AuthInfoV1 {
4647
access: string;

src/atlclients/loginManager.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@ export class LoginManager {
5353
this.saveDetails(provider, site, resp, isOnboarding);
5454
}
5555

56+
public async initRemoteAuth(state: Object) {
57+
this._dancer.doInitRemoteDance(state);
58+
}
59+
60+
public async finishRemoteAuth(code: string): Promise<void> {
61+
const provider = OAuthProvider.JiraCloudRemote;
62+
const site = {
63+
host: 'https://jira.atlassian.com',
64+
product: ProductJira,
65+
};
66+
67+
const resp = await this._dancer.doFinishRemoteDance(provider, site, code);
68+
69+
// TODO: change false here when this is reachable from the onboarding flow
70+
this.saveDetails(provider, site, resp, false);
71+
}
72+
5673
private async saveDetails(provider: OAuthProvider, site: SiteInfo, resp: OAuthResponse, isOnboarding?: boolean) {
5774
try {
5875
const oauthInfo: OAuthInfo = {

src/atlclients/oauthDancer.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@ export class OAuthDancer implements Disposable {
9696
return app;
9797
}
9898

99+
public async doInitRemoteDance(state: any) {
100+
const provider = OAuthProvider.JiraCloudRemote;
101+
const strategy = strategyForProvider(provider);
102+
103+
const stateBase64 = Buffer.from(JSON.stringify(state)).toString('base64');
104+
const uri = vscode.Uri.parse(strategy.authorizeUrl(stateBase64));
105+
vscode.window.showInformationMessage(`Opening browser to ${uri.toString(true)}`);
106+
vscode.env.openExternal(uri);
107+
}
108+
109+
public async doFinishRemoteDance(provider: OAuthProvider, site: SiteInfo, code: string): Promise<OAuthResponse> {
110+
const strategy = strategyForProvider(provider);
111+
const agent = getAgent(site);
112+
const responseHandler = responseHandlerForStrategy(strategy!, agent, this._axios);
113+
const tokens = await responseHandler.tokens(code as string);
114+
const accessibleResources = await responseHandler.accessibleResources(tokens.accessToken);
115+
if (accessibleResources.length === 0) {
116+
throw new Error(`No accessible resources found for ${provider}`);
117+
}
118+
const user = await responseHandler.user(tokens.accessToken, accessibleResources[0]);
119+
120+
return {
121+
access: tokens.accessToken,
122+
refresh: tokens.refreshToken!,
123+
expirationDate: tokens.expiration,
124+
iat: tokens.iat,
125+
receivedAt: tokens.receivedAt,
126+
user: user,
127+
accessibleResources: accessibleResources,
128+
};
129+
}
130+
99131
public async doDance(provider: OAuthProvider, site: SiteInfo, callback: string): Promise<OAuthResponse> {
100132
const currentlyInflight = this._authsInFlight.get(provider);
101133
if (currentlyInflight) {

src/atlclients/strategy.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const expectedData = {
6060
},
6161
tokenRefreshData:
6262
'{"grant_type":"refresh_token","client_id":"bJChVgBQd0aNUPuFZ8YzYBVZz3X4QTe2","refresh_token":"refreshToken"}',
63-
profileUrl: '',
63+
profileUrl: 'https://api.atlassian.com/me',
6464
emailsUrl: '',
6565
},
6666
jiracloudstaging: {
@@ -77,7 +77,7 @@ const expectedData = {
7777
},
7878
tokenRefreshData:
7979
'{"grant_type":"refresh_token","client_id":"pmzXmUav3Rr5XEL0Sie7Biec0WGU8BKg","refresh_token":"refreshToken"}',
80-
profileUrl: '',
80+
profileUrl: 'https://api.stg.atlassian.com/me',
8181
emailsUrl: '',
8282
},
8383
};

0 commit comments

Comments
 (0)