Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AXON-46] Add remote auth flow using atlascode-backend #135

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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