@yuntower/yuntower-account-web-sdk 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # YunTowerAccount-WebSDK
2
+
3
+ 云塔开放认证平台官方Web SDK
4
+
5
+ Apache License 2.0
6
+
7
+ - 官网:[https://account.yuntower.com](https://account.yuntower.cn)
8
+ - 文档:[https://account.yuntower.com/docs](https://account.yuntower.cn/docs)
9
+ - NPM:[https://www.npmjs.com/package/@yuntower/yuntower-account-web-sdk](https://www.npmjs.com/package/yuntower-account-web-sdk)
10
+
11
+
12
+ ```
13
+ // For versions <= 0.0.6
14
+ npm i yuntower-account-web-sdk
15
+
16
+ // For versions >= 0.0.7
17
+ npm i yuntower/yuntower-account-web-sdk
18
+ ```
19
+
20
+ Provide by YunTower
package/index.js ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var YunTowerAccountSDK = /** @class */ (function () {
4
+ function YunTowerAccountSDK(_a) {
5
+ var type = _a.type, appid = _a.appid, _b = _a.redirect_url, redirect_url = _b === void 0 ? null : _b, _c = _a.state, state = _c === void 0 ? null : _c, _d = _a.scope, scope = _d === void 0 ? 'user_profile' : _d;
6
+ if (!appid || !scope) {
7
+ console.error('[YunTowerAccountSDK] 参数缺失');
8
+ }
9
+ if (!['window', 'redirect', 'iframe'].includes(type)) {
10
+ console.error('[YunTowerAccountSDK] [type]参数错误');
11
+ }
12
+ if (!['user_profile'].includes(scope)) {
13
+ console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
14
+ }
15
+ this.auth_status = false;
16
+ this.config = {
17
+ auth: 'https://account.yuntower.com',
18
+ origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
19
+ type: type,
20
+ appid: appid,
21
+ scope: scope,
22
+ redirect_url: redirect_url,
23
+ state: state
24
+ };
25
+ }
26
+ /**
27
+ * 加载授权窗口
28
+ *
29
+ * @param {string} id 目标元素的ID
30
+ * @param {string} style 样式
31
+ * @param callback
32
+ */
33
+ YunTowerAccountSDK.prototype.loadAuthWindow = function (id, style, callback) {
34
+ var _this = this;
35
+ if (style === void 0) { style = ''; }
36
+ var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
37
+ var iframe = document.getElementById(id);
38
+ if (!iframe) {
39
+ console.error('[YunTowerAccountSDK] 未找到id元素');
40
+ return;
41
+ }
42
+ if (this.config.type !== 'iframe') {
43
+ console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
44
+ return;
45
+ }
46
+ if (style === '') {
47
+ style = "height: 366px; width: 400px; border: unset; border-radius: 5px";
48
+ }
49
+ iframe.setAttribute('src', auth_path);
50
+ iframe.setAttribute('style', style);
51
+ // 监听来自子页面的消息
52
+ var messageListener = function (event) {
53
+ var _a, _b, _c, _d;
54
+ var origin = event.origin.replace(/^https?:\/\//, '');
55
+ if (!_this.config.origin_white_list.includes(origin))
56
+ return;
57
+ // 授权成功
58
+ if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
59
+ window.removeEventListener('message', messageListener);
60
+ if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
61
+ _this.auth_status = true;
62
+ callback({
63
+ event: 'auth',
64
+ status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
65
+ data: event.data.data
66
+ });
67
+ }
68
+ else {
69
+ callback({
70
+ event: 'auth',
71
+ status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
72
+ msg: event.data.msg
73
+ });
74
+ }
75
+ }
76
+ };
77
+ window.addEventListener('message', messageListener);
78
+ };
79
+ /**
80
+ * 开启授权窗口
81
+ * @param {*} callback
82
+ */
83
+ YunTowerAccountSDK.prototype.openAuthWindow = function (callback) {
84
+ var _this = this;
85
+ var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
86
+ if (this.config.type == 'redirect') {
87
+ window.location.href = auth_path;
88
+ return false;
89
+ }
90
+ var child = window.open(auth_path, '_blank', 'width=500,height=600');
91
+ // 监听来自子页面的消息
92
+ var messageListener = function (event) {
93
+ var _a, _b, _c, _d;
94
+ var origin = event.origin.replace(/^https?:\/\//, '');
95
+ if (!_this.config.origin_white_list.includes(origin))
96
+ return;
97
+ // 授权成功
98
+ if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
99
+ window.removeEventListener('message', messageListener);
100
+ if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
101
+ _this.auth_status = true;
102
+ callback({
103
+ event: 'auth',
104
+ status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
105
+ data: event.data.data
106
+ });
107
+ }
108
+ else {
109
+ callback({
110
+ event: 'auth',
111
+ status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
112
+ msg: event.data.msg
113
+ });
114
+ }
115
+ child === null || child === void 0 ? void 0 : child.close();
116
+ }
117
+ };
118
+ window.addEventListener('message', messageListener);
119
+ if (child && !child.closed) {
120
+ var timer_1 = setInterval(function () {
121
+ if (child.closed) {
122
+ clearInterval(timer_1);
123
+ callback({
124
+ event: 'closed',
125
+ status: 'success'
126
+ });
127
+ }
128
+ child.postMessage({ action: 'status' }, '*');
129
+ }, 3000);
130
+ }
131
+ };
132
+ return YunTowerAccountSDK;
133
+ }());
134
+ exports.default = YunTowerAccountSDK;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@yuntower/yuntower-account-web-sdk",
3
+ "description": "YunTower Account Web SDK",
4
+ "version": "0.0.7",
5
+ "private": false,
6
+ "author": "yuntower",
7
+ "license": "Apache-2.0",
8
+ "dependencies": {
9
+ "typescript": "^5.5.4"
10
+ },
11
+ "main": "index.js",
12
+ "scripts": {
13
+ "test": "echo \"Error: no test specified\" && exit 1"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ }
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var YunTowerAccountSDK = /** @class */ (function () {
4
+ function YunTowerAccountSDK(_a) {
5
+ var type = _a.type, appid = _a.appid, _b = _a.redirect_url, redirect_url = _b === void 0 ? null : _b, _c = _a.state, state = _c === void 0 ? null : _c, _d = _a.scope, scope = _d === void 0 ? 'user_profile' : _d;
6
+ if (!appid || !scope) {
7
+ console.error('[YunTowerAccountSDK] 参数缺失');
8
+ }
9
+ if (!['window', 'redirect', 'iframe'].includes(type)) {
10
+ console.error('[YunTowerAccountSDK] [type]参数错误');
11
+ }
12
+ if (!['user_profile'].includes(scope)) {
13
+ console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
14
+ }
15
+ this.auth_status = false;
16
+ this.config = {
17
+ auth: 'https://account.yuntower.com',
18
+ origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
19
+ type: type,
20
+ appid: appid,
21
+ scope: scope,
22
+ redirect_url: redirect_url,
23
+ state: state
24
+ };
25
+ }
26
+ /**
27
+ * 加载授权窗口
28
+ *
29
+ * @param {string} id 目标元素的ID
30
+ * @param {string} style 样式
31
+ * @param callback
32
+ */
33
+ YunTowerAccountSDK.prototype.loadAuthWindow = function (id, style, callback) {
34
+ var _this = this;
35
+ if (style === void 0) { style = ''; }
36
+ var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
37
+ var iframe = document.getElementById(id);
38
+ if (!iframe) {
39
+ console.error('[YunTowerAccountSDK] 未找到id元素');
40
+ return;
41
+ }
42
+ if (this.config.type !== 'iframe') {
43
+ console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
44
+ return;
45
+ }
46
+ if (style === '') {
47
+ style = "height: 366px; width: 400px; border: unset; border-radius: 5px";
48
+ }
49
+ iframe.setAttribute('src', auth_path);
50
+ iframe.setAttribute('style', style);
51
+ // 监听来自子页面的消息
52
+ var messageListener = function (event) {
53
+ var _a, _b, _c, _d;
54
+ var origin = event.origin.replace(/^https?:\/\//, '');
55
+ if (!_this.config.origin_white_list.includes(origin))
56
+ return;
57
+ // 授权成功
58
+ if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
59
+ window.removeEventListener('message', messageListener);
60
+ if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
61
+ _this.auth_status = true;
62
+ callback({
63
+ event: 'auth',
64
+ status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
65
+ data: event.data.data
66
+ });
67
+ }
68
+ else {
69
+ callback({
70
+ event: 'auth',
71
+ status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
72
+ msg: event.data.msg
73
+ });
74
+ }
75
+ }
76
+ };
77
+ window.addEventListener('message', messageListener);
78
+ };
79
+ /**
80
+ * 开启授权窗口
81
+ * @param {*} callback
82
+ */
83
+ YunTowerAccountSDK.prototype.openAuthWindow = function (callback) {
84
+ var _this = this;
85
+ var auth_path = "".concat(this.config.auth, "/auth/app?type=").concat(this.config.type, "&appid=").concat(this.config.appid, "&redirect_url=").concat(this.config.redirect_url, "&scope=").concat(this.config.scope, "&state=").concat(this.config.state);
86
+ if (this.config.type == 'redirect') {
87
+ window.location.href = auth_path;
88
+ return false;
89
+ }
90
+ var child = window.open(auth_path, '_blank', 'width=500,height=600');
91
+ // 监听来自子页面的消息
92
+ var messageListener = function (event) {
93
+ var _a, _b, _c, _d;
94
+ var origin = event.origin.replace(/^https?:\/\//, '');
95
+ if (!_this.config.origin_white_list.includes(origin))
96
+ return;
97
+ // 授权成功
98
+ if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.action) === 'status') {
99
+ window.removeEventListener('message', messageListener);
100
+ if (((_b = event.data) === null || _b === void 0 ? void 0 : _b.status) === 'success') {
101
+ _this.auth_status = true;
102
+ callback({
103
+ event: 'auth',
104
+ status: (_c = event.data) === null || _c === void 0 ? void 0 : _c.status,
105
+ data: event.data.data
106
+ });
107
+ }
108
+ else {
109
+ callback({
110
+ event: 'auth',
111
+ status: (_d = event.data) === null || _d === void 0 ? void 0 : _d.status,
112
+ msg: event.data.msg
113
+ });
114
+ }
115
+ child === null || child === void 0 ? void 0 : child.close();
116
+ }
117
+ };
118
+ window.addEventListener('message', messageListener);
119
+ if (child && !child.closed) {
120
+ var timer_1 = setInterval(function () {
121
+ if (child.closed) {
122
+ clearInterval(timer_1);
123
+ callback({
124
+ event: 'closed',
125
+ status: 'success'
126
+ });
127
+ }
128
+ child.postMessage({ action: 'status' }, '*');
129
+ }, 3000);
130
+ }
131
+ };
132
+ return YunTowerAccountSDK;
133
+ }());
134
+ exports.default = YunTowerAccountSDK;
package/src/index.ts ADDED
@@ -0,0 +1,181 @@
1
+ class YunTowerAccountSDK {
2
+ auth_status: boolean;
3
+ config: {
4
+ auth: string,
5
+ origin_white_list: string[];
6
+ type: 'window' | 'redirect' | 'iframe';
7
+ appid: string;
8
+ scope: string | '';
9
+ redirect_url: null | string;
10
+ state: null | string;
11
+ };
12
+
13
+ constructor({
14
+ type,
15
+ appid,
16
+ redirect_url = null,
17
+ state = null,
18
+ scope = 'user_profile'
19
+ }: {
20
+ type: 'window' | 'redirect' | 'iframe';
21
+ appid: string;
22
+ scope: string | 'user_profile';
23
+ redirect_url?: null | string;
24
+ state?: null | string;
25
+ }) {
26
+ if (!appid || !scope) {
27
+ console.error('[YunTowerAccountSDK] 参数缺失');
28
+ }
29
+
30
+ if (!['window', 'redirect', 'iframe'].includes(type)) {
31
+ console.error('[YunTowerAccountSDK] [type]参数错误');
32
+ }
33
+
34
+ if (!['user_profile'].includes(scope)) {
35
+ console.error('[YunTowerAccountSDK] [scope]参数错误,目前只支持[user_profile]');
36
+ }
37
+
38
+ this.auth_status = false;
39
+ this.config = {
40
+ auth: 'https://account.yuntower.com',
41
+ origin_white_list: ['account.yuntower.cn', 'account.yuntower.com'],
42
+ type,
43
+ appid,
44
+ scope,
45
+ redirect_url,
46
+ state
47
+ };
48
+ }
49
+
50
+ /**
51
+ * 加载授权窗口
52
+ *
53
+ * @param {string} id 目标元素的ID
54
+ * @param {string} style 样式
55
+ * @param callback
56
+ */
57
+ loadAuthWindow(id: string, style: string = '', callback: (arg0: {
58
+ event: string;
59
+ status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
60
+ data?: any;
61
+ msg?: string;
62
+ }) => void) {
63
+ const auth_path = `${this.config.auth}/auth/app?type=${this.config.type}&appid=${this.config.appid}&redirect_url=${this.config.redirect_url}&scope=${this.config.scope}&state=${this.config.state}`;
64
+ const iframe = document.getElementById(id);
65
+ if (!iframe) {
66
+ console.error('[YunTowerAccountSDK] 未找到id元素');
67
+ return;
68
+ }
69
+
70
+ if (this.config.type !== 'iframe') {
71
+ console.error('[YunTowerAccountSDK] type 参数错误,仅支持[iframe]类型');
72
+ return;
73
+ }
74
+
75
+ if (style === '') {
76
+ style = "height: 366px; width: 400px; border: unset; border-radius: 5px"
77
+ }
78
+
79
+
80
+ iframe.setAttribute('src', auth_path);
81
+ iframe.setAttribute('style', style);
82
+
83
+ // 监听来自子页面的消息
84
+ const messageListener = (event: MessageEvent) => {
85
+ const origin = event.origin.replace(/^https?:\/\//, '');
86
+
87
+ if (!this.config.origin_white_list.includes(origin)) return;
88
+
89
+ // 授权成功
90
+ if (event.data?.action === 'status') {
91
+ window.removeEventListener('message', messageListener);
92
+ if (event.data?.status === 'success') {
93
+ this.auth_status = true;
94
+ callback({
95
+ event: 'auth',
96
+ status: event.data?.status,
97
+ data: event.data.data
98
+ });
99
+ } else {
100
+ callback({
101
+ event: 'auth',
102
+ status: event.data?.status,
103
+ msg: event.data.msg
104
+ });
105
+ }
106
+ }
107
+ };
108
+
109
+ window.addEventListener('message', messageListener);
110
+ }
111
+
112
+ /**
113
+ * 开启授权窗口
114
+ * @param {*} callback
115
+ */
116
+ openAuthWindow(callback: (arg0: {
117
+ event: string;
118
+ status: 'success' | 'failed' | 'error' | 'noLogin' | 'denied';
119
+ data?: any;
120
+ msg?: string;
121
+ }) => void) {
122
+ const auth_path = `${this.config.auth}/auth/app?type=${this.config.type}&appid=${this.config.appid}&redirect_url=${this.config.redirect_url}&scope=${this.config.scope}&state=${this.config.state}`;
123
+
124
+ if (this.config.type == 'redirect') {
125
+ window.location.href = auth_path;
126
+ return false;
127
+ }
128
+
129
+ let child = window.open(
130
+ auth_path,
131
+ '_blank',
132
+ 'width=500,height=600'
133
+ );
134
+
135
+
136
+ // 监听来自子页面的消息
137
+ const messageListener = (event: MessageEvent) => {
138
+ const origin = event.origin.replace(/^https?:\/\//, '');
139
+
140
+ if (!this.config.origin_white_list.includes(origin)) return;
141
+
142
+ // 授权成功
143
+ if (event.data?.action === 'status') {
144
+ window.removeEventListener('message', messageListener);
145
+ if (event.data?.status === 'success') {
146
+ this.auth_status = true;
147
+ callback({
148
+ event: 'auth',
149
+ status: event.data?.status,
150
+ data: event.data.data
151
+ });
152
+ } else {
153
+ callback({
154
+ event: 'auth',
155
+ status: event.data?.status,
156
+ msg: event.data.msg
157
+ });
158
+ }
159
+ child?.close();
160
+ }
161
+ };
162
+
163
+ window.addEventListener('message', messageListener);
164
+
165
+
166
+ if (child && !child.closed) {
167
+ const timer = setInterval(() => {
168
+ if (child.closed) {
169
+ clearInterval(timer);
170
+ callback({
171
+ event: 'closed',
172
+ status: 'success'
173
+ });
174
+ }
175
+ child.postMessage({action: 'status'}, '*');
176
+ }, 3000);
177
+ }
178
+ }
179
+ }
180
+
181
+ export default YunTowerAccountSDK;
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "module": "commonjs",
5
+ "lib": ["es2020", "dom", "es2015.promise"],
6
+ "strict": true,
7
+ "esModuleInterop": true,
8
+ "outDir": "./dist"
9
+ },
10
+ "include": [
11
+ "src/**/*"
12
+ ],
13
+ "exclude": [
14
+ "node_modules",
15
+ "**/*.spec.ts"
16
+ ]
17
+ }