Skip to content

Commit

Permalink
[AXON-46] Add remote auth flow using atlascode-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
sdzh-atlassian committed Feb 20, 2025
1 parent b148985 commit 93c9fa8
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 232 deletions.
1 change: 1 addition & 0 deletions src/atlclients/authInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export enum OAuthProvider {
BitbucketCloudStaging = 'bbcloudstaging',
JiraCloud = 'jiracloud',
JiraCloudStaging = 'jiracloudstaging',
JiraCloudRemote = 'jiracloudremote',
}
export interface AuthInfoV1 {
access: string;
Expand Down
17 changes: 17 additions & 0 deletions src/atlclients/loginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ export class LoginManager {
this.saveDetails(provider, site, resp, isOnboarding);
}

public async initRemoteAuth(state: Object) {
this._dancer.doInitRemoteDance(state);
}

public async finishRemoteAuth(code: string): Promise<void> {
const provider = OAuthProvider.JiraCloudRemote;
const site = {
host: 'https://jira.atlassian.com',
product: ProductJira,
};

const resp = await this._dancer.doFinishRemoteDance(provider, site, code);

// TODO: change false here when this is reachable from the onboarding flow
this.saveDetails(provider, site, resp, false);
}

private async saveDetails(provider: OAuthProvider, site: SiteInfo, resp: OAuthResponse, isOnboarding?: boolean) {
try {
const oauthInfo: OAuthInfo = {
Expand Down
32 changes: 32 additions & 0 deletions src/atlclients/oauthDancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ export class OAuthDancer implements Disposable {
return app;
}

public async doInitRemoteDance(state: any) {
const provider = OAuthProvider.JiraCloudRemote;
const strategy = strategyForProvider(provider);

const stateBase64 = Buffer.from(JSON.stringify(state)).toString('base64');
const uri = vscode.Uri.parse(strategy.authorizeUrl(stateBase64));
vscode.window.showInformationMessage(`Opening browser to ${uri.toString(true)}`);
vscode.env.openExternal(uri);
}

public async doFinishRemoteDance(provider: OAuthProvider, site: SiteInfo, code: string): Promise<OAuthResponse> {
const strategy = strategyForProvider(provider);
const agent = getAgent(site);
const responseHandler = responseHandlerForStrategy(strategy!, agent, this._axios);
const tokens = await responseHandler.tokens(code as string);
const accessibleResources = await responseHandler.accessibleResources(tokens.accessToken);
if (accessibleResources.length === 0) {
throw new Error(`No accessible resources found for ${provider}`);
}
const user = await responseHandler.user(tokens.accessToken, accessibleResources[0]);

return {
access: tokens.accessToken,
refresh: tokens.refreshToken!,
expirationDate: tokens.expiration,
iat: tokens.iat,
receivedAt: tokens.receivedAt,
user: user,
accessibleResources: accessibleResources,
};
}

public async doDance(provider: OAuthProvider, site: SiteInfo, callback: string): Promise<OAuthResponse> {
const currentlyInflight = this._authsInFlight.get(provider);
if (currentlyInflight) {
Expand Down
4 changes: 2 additions & 2 deletions src/atlclients/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const expectedData = {
},
tokenRefreshData:
'{"grant_type":"refresh_token","client_id":"bJChVgBQd0aNUPuFZ8YzYBVZz3X4QTe2","refresh_token":"refreshToken"}',
profileUrl: '',
profileUrl: 'https://api.atlassian.com/me',
emailsUrl: '',
},
jiracloudstaging: {
Expand All @@ -77,7 +77,7 @@ const expectedData = {
},
tokenRefreshData:
'{"grant_type":"refresh_token","client_id":"pmzXmUav3Rr5XEL0Sie7Biec0WGU8BKg","refresh_token":"refreshToken"}',
profileUrl: '',
profileUrl: 'https://api.stg.atlassian.com/me',
emailsUrl: '',
},
};
Expand Down
Loading

0 comments on commit 93c9fa8

Please sign in to comment.