@newgameplusinc/odyssey-sso 1.0.2 → 2.0.0
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/dist/config.d.ts +27 -0
- package/dist/config.js +31 -0
- package/dist/{lib/database.d.ts → database.d.ts} +1 -1
- package/dist/{lib/database.js → database.js} +4 -2
- package/dist/main.d.ts +249 -0
- package/dist/main.js +948 -0
- package/dist/types.d.ts +466 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +1 -0
- package/package.json +7 -10
- package/README.md +0 -3
- package/dist/config/index.d.ts +0 -7
- package/dist/config/index.js +0 -7
- package/dist/index.d.ts +0 -27
- package/dist/index.js +0 -158
- package/dist/lib/axios.d.ts +0 -6
- package/dist/lib/axios.js +0 -20
package/dist/index.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
import * as constants from "./config";
|
|
2
|
-
import Axios from "./lib/axios";
|
|
3
|
-
import Database from "./lib/database";
|
|
4
|
-
export default class OdysseySSO extends Axios {
|
|
5
|
-
constructor({ apiKey, clientId, debug = false, }) {
|
|
6
|
-
super(apiKey, clientId);
|
|
7
|
-
this.isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
8
|
-
this.triggerSignIn = async (callbackUrl) => {
|
|
9
|
-
if (!this.isBrowser) {
|
|
10
|
-
if (this.debug)
|
|
11
|
-
console.warn("OdysseySSO: triggerSignIn is not supported in Node.js, please use triggerSignIn in the browser.");
|
|
12
|
-
return;
|
|
13
|
-
}
|
|
14
|
-
const url = `${constants.SSO_CLIENT_URL}/sso?client_id=${this.clientId}&redirect_uri=${callbackUrl}&api_key=${this.apiKey}`;
|
|
15
|
-
window.location.href = url;
|
|
16
|
-
};
|
|
17
|
-
this.exchangeCall = async (token, clientSecret) => {
|
|
18
|
-
return await this.api
|
|
19
|
-
.post("/api/v1/sso/exchange", {
|
|
20
|
-
token,
|
|
21
|
-
clientSecret,
|
|
22
|
-
sdkKey: this.apiKey,
|
|
23
|
-
})
|
|
24
|
-
.then((res) => {
|
|
25
|
-
const { accessToken, refreshToken } = (res.data?.data?.tokens ??
|
|
26
|
-
{});
|
|
27
|
-
return {
|
|
28
|
-
isSuccess: true,
|
|
29
|
-
data: {
|
|
30
|
-
accessToken,
|
|
31
|
-
refreshToken,
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
})
|
|
35
|
-
.catch((err) => {
|
|
36
|
-
if (this.debug)
|
|
37
|
-
console.error(err);
|
|
38
|
-
return {
|
|
39
|
-
isSuccess: false,
|
|
40
|
-
data: null,
|
|
41
|
-
};
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
/**
|
|
45
|
-
* @param code - the token you got from the callback
|
|
46
|
-
* @param clientSecret - keep it empty for client side exchange, for better security use clientSecret from your server
|
|
47
|
-
*/
|
|
48
|
-
this.exchangeCode = async (redirectPath) => {
|
|
49
|
-
if (!this.isBrowser)
|
|
50
|
-
return;
|
|
51
|
-
if (this.debug)
|
|
52
|
-
console.warn("OdysseySSO: for better security, please use exchangeCode in the server using clientSecret.");
|
|
53
|
-
const p = new URLSearchParams(window.location.search);
|
|
54
|
-
const token = p.get("token");
|
|
55
|
-
if (!token) {
|
|
56
|
-
if (this.debug)
|
|
57
|
-
console.warn("OdysseySSO: no token found in the url.");
|
|
58
|
-
return {
|
|
59
|
-
isSuccess: false,
|
|
60
|
-
data: null,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
const res = await this.exchangeCall(token);
|
|
64
|
-
if (res.isSuccess) {
|
|
65
|
-
await this.db.delete(this.clientId);
|
|
66
|
-
const { data: profile } = await this.api
|
|
67
|
-
.get("/api/v1/users/profile", {
|
|
68
|
-
headers: {
|
|
69
|
-
authorization: `Bearer ${res.data.accessToken}`,
|
|
70
|
-
},
|
|
71
|
-
})
|
|
72
|
-
.then((res) => res.data)
|
|
73
|
-
.catch(() => ({ data: null }));
|
|
74
|
-
if (!profile)
|
|
75
|
-
return;
|
|
76
|
-
delete profile.id;
|
|
77
|
-
await this.db.create({
|
|
78
|
-
id: this.clientId,
|
|
79
|
-
...profile,
|
|
80
|
-
accessToken: res.data.accessToken,
|
|
81
|
-
refreshToken: res.data.refreshToken,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
if (this.debug)
|
|
85
|
-
console.error("OdysseySSO: exchange failed", res);
|
|
86
|
-
if (redirectPath)
|
|
87
|
-
window.location.href = redirectPath;
|
|
88
|
-
};
|
|
89
|
-
this.getProfileData = async () => {
|
|
90
|
-
if (!this.isBrowser) {
|
|
91
|
-
if (this.debug)
|
|
92
|
-
console.warn("OdysseySSO: getProfileData is not supported in Node.js, please use getProfileData in the browser.");
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
const user = (await this.db.getById(this.clientId));
|
|
96
|
-
if (!user)
|
|
97
|
-
return null;
|
|
98
|
-
return this.api
|
|
99
|
-
.get("/api/v1/users/profile", {
|
|
100
|
-
headers: {
|
|
101
|
-
authorization: `Bearer ${user.accessToken}`,
|
|
102
|
-
},
|
|
103
|
-
})
|
|
104
|
-
.then((res) => res.data.data)
|
|
105
|
-
.catch(() => ({}));
|
|
106
|
-
};
|
|
107
|
-
this.logout = async () => {
|
|
108
|
-
if (!this.isBrowser)
|
|
109
|
-
return;
|
|
110
|
-
await this.db.delete(this.clientId);
|
|
111
|
-
};
|
|
112
|
-
this.refreshToken = async () => {
|
|
113
|
-
if (!this.isBrowser)
|
|
114
|
-
return;
|
|
115
|
-
const user = (await this.db.getById(this.clientId));
|
|
116
|
-
if (!user)
|
|
117
|
-
return;
|
|
118
|
-
const { refreshToken } = user;
|
|
119
|
-
const { data } = await this.api
|
|
120
|
-
.post("/api/v1/users/refresh", {
|
|
121
|
-
token: refreshToken,
|
|
122
|
-
})
|
|
123
|
-
.then((res) => res.data)
|
|
124
|
-
.catch(() => ({ data: null }));
|
|
125
|
-
if (!data)
|
|
126
|
-
return;
|
|
127
|
-
const { data: profile } = await this.api
|
|
128
|
-
.get("/api/v1/users/profile", {
|
|
129
|
-
headers: {
|
|
130
|
-
authorization: `Bearer ${data.accessToken}`,
|
|
131
|
-
},
|
|
132
|
-
})
|
|
133
|
-
.then((res) => res.data)
|
|
134
|
-
.catch(() => ({ data: null }));
|
|
135
|
-
if (!profile)
|
|
136
|
-
return;
|
|
137
|
-
delete profile.id;
|
|
138
|
-
await this.db.update(this.clientId, {
|
|
139
|
-
...profile,
|
|
140
|
-
accessToken: data.accessToken,
|
|
141
|
-
refreshToken: data.refreshToken,
|
|
142
|
-
});
|
|
143
|
-
};
|
|
144
|
-
this.updateAvatar = async (avatar) => {
|
|
145
|
-
return this.api.post("/users/avatar", { avatar });
|
|
146
|
-
};
|
|
147
|
-
this.debug = debug;
|
|
148
|
-
this.clientId = clientId;
|
|
149
|
-
this.apiKey = apiKey;
|
|
150
|
-
this.db = new Database({
|
|
151
|
-
dbName: constants.DB_NAME,
|
|
152
|
-
dbVersion: constants.DB_VERSION,
|
|
153
|
-
storeName: constants.STORE_NAME,
|
|
154
|
-
});
|
|
155
|
-
if (!this.isBrowser)
|
|
156
|
-
this.db.open();
|
|
157
|
-
}
|
|
158
|
-
}
|
package/dist/lib/axios.d.ts
DELETED
package/dist/lib/axios.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import { SSO_SERVER_URL, SDK_NAME, SDK_VERSION } from "../config";
|
|
3
|
-
export default class Axios {
|
|
4
|
-
constructor(apiKey, clientId) {
|
|
5
|
-
this.setAccessToken = (accessToken) => {
|
|
6
|
-
this.api.defaults.headers.authorization = `Bearer ${accessToken}`;
|
|
7
|
-
};
|
|
8
|
-
this.api = axios.create({
|
|
9
|
-
baseURL: SSO_SERVER_URL,
|
|
10
|
-
timeout: 10000,
|
|
11
|
-
headers: {
|
|
12
|
-
"x-client-name": SDK_NAME,
|
|
13
|
-
"x-client-version": SDK_VERSION,
|
|
14
|
-
"x-api-key": apiKey,
|
|
15
|
-
"x-client-id": clientId,
|
|
16
|
-
},
|
|
17
|
-
// withCredentials: true, // todo: enable back later once the cors is fixed on the backend
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|