Skip to content

Commit b6246aa

Browse files
committed
feat: 授权登陆
1 parent cb1f2ec commit b6246aa

5 files changed

Lines changed: 96 additions & 8 deletions

File tree

packages/code-api/src/atomgit/atomgit.service.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { API as ConflictAPI } from '../antcode/types';
2020
import { request, RequestOptions } from '@alipay/alex-shared';
2121
import { CODE_PLATFORM_CONFIG, HelperService } from '../common';
22-
import { URI, MessageType } from '@opensumi/ide-core-common';
22+
import { URI, MessageType, isObject } from '@opensumi/ide-core-common';
2323
import { API } from './types';
2424

2525
@Injectable()
@@ -32,14 +32,54 @@ export class AtomGitAPIService implements ICodeAPIService {
3232
private _PRIVATE_TOKEN: string | null;
3333

3434
get PRIVATE_TOKEN() {
35-
return this._PRIVATE_TOKEN;
35+
return this._PRIVATE_TOKEN || this.helper.ATOMGIT_TOKEN;
3636
}
3737

3838
constructor() {
39-
this._PRIVATE_TOKEN = this.config.token || 'atu_7d36d5a52af4156b76d6f61b240d72cc';
39+
this._PRIVATE_TOKEN = this.config.token || this.helper.ATOMGIT_TOKEN || '';
40+
}
41+
42+
private async checkAccessToken(): Promise<boolean> {
43+
const popupWindow = window.open(`${this.config.origin}/login/oauth/authorize?client_id=9d8b531661f441d1`, '_blank', 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=800,height=520,top=150,left=150');
44+
return new Promise<boolean>((resolve, reject) => {
45+
const handleMessage = async (event: MessageEvent) => {
46+
try {
47+
const { data } = event;
48+
if (isObject(data) && data.type === 'atomgit') {
49+
const { data: { code } } = data;
50+
popupWindow?.close();
51+
if (!code) {
52+
resolve(false);
53+
return;
54+
}
55+
const tokenResult: any = await this.request('http://svc-ldh2u2i3brw4lg1e.cloudide.svc.et15-sqa.alipay.net:7001/openapi/atomgit-auth-callback', {
56+
baseURL: '',
57+
method: 'post',
58+
credentials: 'omit',
59+
data: {
60+
code
61+
}
62+
});
63+
if (tokenResult && tokenResult.access_token) {
64+
this.helper.ATOMGIT_TOKEN = tokenResult.access_token;
65+
resolve(true);
66+
return;
67+
}
68+
resolve(false);
69+
}
70+
} catch (error) {
71+
reject(error);
72+
}
73+
};
74+
window.addEventListener('message', handleMessage);
75+
});
4076
}
4177

4278
public async available(): Promise<boolean> {
79+
const token = this._PRIVATE_TOKEN;
80+
if (!token) {
81+
return await this.checkAccessToken();
82+
}
4383
return true;
4484
}
4585

@@ -57,7 +97,7 @@ export class AtomGitAPIService implements ICodeAPIService {
5797
const { headers, ...rest } = options || {};
5898
const privateToken = this.PRIVATE_TOKEN;
5999
return await request(path, {
60-
baseURL: this.config.endpoint,
100+
baseURL: options?.baseURL ?? this.config.endpoint,
61101
responseType: 'json',
62102
headers: {
63103
...(privateToken
@@ -74,6 +114,8 @@ export class AtomGitAPIService implements ICodeAPIService {
74114
let messageKey = 'error.request';
75115
if (status === 401) {
76116
messageKey = 'atomgit.unauthorized';
117+
// 401 的情况再登陆一次
118+
await this.checkAccessToken();
77119
} else if (status === 404) {
78120
messageKey = 'error.resource-not-found';
79121
}
@@ -136,6 +178,10 @@ export class AtomGitAPIService implements ICodeAPIService {
136178
throw new Error('Method not implemented.');
137179
}
138180
async getBranches(repo: IRepositoryModel): Promise<BranchOrTag[]> {
181+
if (!this._PRIVATE_TOKEN) {
182+
return [];
183+
}
184+
139185
const branches = await this.request<API.ResponseBranchesInfo[]>(`/repos/${this.getProjectPath(repo)}/branches`);
140186
if (!Array.isArray(branches)) {
141187
throw new Error('[can not find branch list]');
@@ -210,7 +256,6 @@ export class AtomGitAPIService implements ICodeAPIService {
210256
}
211257
});
212258

213-
console.log('blamePart:>>>>', blamePart)
214259
return new TextEncoder().encode(JSON.stringify(blamePart));
215260
}
216261
getCommits(_repo: IRepositoryModel, _params: CommitParams): Promise<CommitRecord[]> {

packages/code-api/src/common/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ export const CODE_PLATFORM_CONFIG: Record<ICodePlatform, ICodePlatformConfig> =
119119
platform: CodePlatform.atomgit,
120120
hostname: ['atomgit.com'],
121121
origin: 'https://atomgit.com/',
122-
// endpoint: 'https://api.atomgit.com',
123-
endpoint: 'https://test-api.atomgit.com',
122+
endpoint: 'https://api.atomgit.com',
124123
brand: 'AtomGit',
125124
line: {
126125
parse(hash: string) {

packages/integrations/src/startup/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const App = () => (
137137
typescript,
138138
codeservice,
139139
gitlens,
140-
graph,
140+
// graph,
141141
imagePreview,
142142
webSCM,
143143
referencesView,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>AtomGit CloudIDE 应用授权</title>
6+
<style>
7+
html {
8+
overflow: hidden;
9+
overscroll-behavior-x: none;
10+
}
11+
body {
12+
margin: 0;
13+
}
14+
#main {
15+
width: 100vw;
16+
height: 100vh;
17+
margin: 0;
18+
padding: 0;
19+
overflow: hidden;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div id="atomgit-main">登陆中...</div>
25+
</body>
26+
<script>
27+
window.addEventListener('load', () => {
28+
const searchParams = new URLSearchParams(window.location.search);
29+
const code = searchParams.get('code');
30+
if (code) {
31+
window.opener.postMessage({
32+
type: 'atomgit',
33+
data: { code }
34+
}, '*');
35+
}
36+
});
37+
</script>
38+
</html>

packages/toolkit/webpack/config.integration.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ module.exports = (option) => {
239239
publicPath: '/',
240240
integration: process.env.INTEGRATION,
241241
}),
242+
new HtmlWebpackPlugin({
243+
filename: 'atomgit-auth.html',
244+
template: option.template || path.join(__dirname, '../public/atomgit-auth.html'),
245+
publicPath: '/atomgit-auth',
246+
integration: process.env.INTEGRATION,
247+
}),
242248
new FriendlyErrorsWebpackPlugin({
243249
compilationSuccessInfo: {
244250
messages: [`Your application is running here: ${baseURL}`],

0 commit comments

Comments
 (0)