Skip to content

Commit d25ee09

Browse files
author
dongfengtao
committed
fix:高教社 静默登录
1 parent f4aeb2d commit d25ee09

5 files changed

Lines changed: 80 additions & 8 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@authing/sso",
3-
"version": "2.1.29",
3+
"version": "2.1.30-alpha.1",
44
"description": "Authing SSO SDK 为开发者提供了简单易用的函数来实现 Web 端的单点登录效果,你可以通过调用 SDK 与 Authing 完成集成,为你的多个业务软件实现浏览器内的单点登录效果。",
55
"main": "build/umd",
66
"typings": "build/types",

src/example.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import { AuthingSSO } from './index'
22

33
const auth = new AuthingSSO({
4-
appId: 'AUTHING_APP_ID',
4+
appId: '60dd57882376f4bb789dbfe1',
55

66
// SSO 应用面板地址
7-
origin: 'https://{SSO 应用面板地址}.authing.cn',
7+
origin: 'https://hep-center.u2.hep.com.cn',
88

99
// 应用登录回调 URL
10-
redirectUri: 'http://localhost:3001/login/callback'
10+
redirectUri: 'http://localhost:3004/'
1111
})
1212

1313
window.onload = async function () {
1414
let res = await auth.trackSession()
15+
let etextbookproRes = await auth.getEtextbookproAccessTokenSilently()
16+
console.log(etextbookproRes,'etextbookproRes')
1517
if (res.session !== null) {
1618
document.getElementById('h1-user-info').style.display = 'block'
1719
document.getElementById('user-info').innerHTML = JSON.stringify(res.userInfo, null, 4)
@@ -25,8 +27,7 @@ window.onload = async function () {
2527
document.getElementById('btn-login').addEventListener('click', function () {
2628
auth.login({
2729
scope: 'openid profile email phone',
28-
responseMode: 'fragment',
29-
responseType: 'code token',
30+
responseType: 'code',
3031
state: Math.random().toString(),
3132
nonce: Math.random().toString()
3233
})

src/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,53 @@ export class AuthingSSO {
407407
});
408408
return res.data;
409409
}
410+
411+
412+
async loginEtextbookpro() {
413+
const ext_idp_conn_id = '69c4acdc5e538db374a7021e';
414+
this.authzUrlBuilder.reset();
415+
let url = this.authzUrlBuilder
416+
.redirectUri(this.redirectUri)
417+
.scope("openid profile email phone")
418+
.responseType('code')
419+
.clientId(this.appId)
420+
.state(Math.random().toString())
421+
.nonce(Math.random().toString())
422+
.extIdpConnId(ext_idp_conn_id)
423+
.build();
424+
425+
console.log(url,'url.hrefhrefhrefhref')
426+
427+
if (isInElectron) {
428+
window.open(url.href);
429+
} else {
430+
window.location.href = url.href;
431+
}
432+
}
433+
434+
435+
async getEtextbookproAccessTokenSilently() {
436+
const referrer = document.referrer;
437+
// const referrer = 'https://lifelong.smartedu.cn/home';
438+
if(referrer.includes('lifelong')) {
439+
440+
try {
441+
const tokenResult = await this.getAccessTokenSilently();
442+
const { id_token, access_token } = tokenResult as {
443+
id_token: string;
444+
access_token: string;
445+
};
446+
447+
if(access_token) {
448+
return { id_token, access_token };
449+
} else {
450+
this.loginEtextbookpro();
451+
}
452+
} catch (silentLoginError) {
453+
this.loginEtextbookpro();
454+
}
455+
} else {
456+
return null;
457+
}
458+
}
410459
}

src/lib/AuthzUrlBuilder.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class AuthzUrlBuilder {
1212
private _loginHint: string;
1313
private _scope: string = "openid email phone profile offline_access";
1414
private _loginPageContext: string;
15+
private _extIdpConnId: string;
1516

1617
constructor(origin: string, appid: string, redirectUri: string) {
1718
if (!origin) {
@@ -57,6 +58,10 @@ export class AuthzUrlBuilder {
5758
if (this._loginHint) {
5859
urls.searchParams.append("login_hint", this._loginHint);
5960
}
61+
62+
if (this._extIdpConnId) {
63+
urls.searchParams.append("ext_idp_conn_id", this._extIdpConnId);
64+
}
6065
return urls;
6166
}
6267

@@ -123,4 +128,21 @@ export class AuthzUrlBuilder {
123128
this._loginPageContext = context;
124129
return this;
125130
}
131+
132+
extIdpConnId(extIdpConnId: string) {
133+
this._extIdpConnId = extIdpConnId;
134+
return this;
135+
}
136+
reset() {
137+
this._responseType = "code";
138+
this._responseMode = "fragment";
139+
this._prompt = undefined;
140+
this._state = Math.random().toString();
141+
this._nonce = undefined;
142+
this._loginHint = undefined;
143+
this._scope = "openid email phone profile offline_access";
144+
this._loginPageContext = undefined;
145+
this._extIdpConnId = undefined;
146+
return this;
147+
}
126148
}

0 commit comments

Comments
 (0)