@arv-bedrock/auth-sso 0.0.1
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/cjs/_fetch.js +33 -0
- package/dist/cjs/auth.js +155 -0
- package/dist/cjs/index.js +35 -0
- package/dist/cjs/types/_fetch.d.ts +6 -0
- package/dist/cjs/types/_fetch.d.ts.map +1 -0
- package/dist/cjs/types/auth.d.ts +46 -0
- package/dist/cjs/types/auth.d.ts.map +1 -0
- package/dist/cjs/types/index.d.ts +18 -0
- package/dist/cjs/types/index.d.ts.map +1 -0
- package/dist/esm/_fetch.js +27 -0
- package/dist/esm/auth.js +142 -0
- package/dist/esm/index.mjs +21 -0
- package/dist/esm/types/_fetch.d.ts +6 -0
- package/dist/esm/types/_fetch.d.ts.map +1 -0
- package/dist/esm/types/auth.d.ts +46 -0
- package/dist/esm/types/auth.d.ts.map +1 -0
- package/dist/esm/types/index.d.ts +18 -0
- package/dist/esm/types/index.d.ts.map +1 -0
- package/package.json +48 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const defaultHeaders = {
|
|
13
|
+
'Content-Type': 'application/json',
|
|
14
|
+
};
|
|
15
|
+
function customFetch(url, options = {}) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const headers = Object.assign(Object.assign({}, defaultHeaders), options.headers);
|
|
18
|
+
const requestOptions = Object.assign(Object.assign({}, options), { credentials: 'include', mode: "cors" });
|
|
19
|
+
requestOptions.headers = headers;
|
|
20
|
+
const response = yield fetch(`${url}`, requestOptions);
|
|
21
|
+
if (!response.ok) {
|
|
22
|
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
23
|
+
}
|
|
24
|
+
const contentType = response.headers.get('content-type');
|
|
25
|
+
if (contentType && contentType.includes('application/json')) {
|
|
26
|
+
return response.json();
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
exports.default = customFetch;
|
package/dist/cjs/auth.js
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const _fetch_1 = __importDefault(require("./_fetch"));
|
|
16
|
+
const PEOPLE_PATH = "people";
|
|
17
|
+
const CLIENT_SECRET = "client_secret";
|
|
18
|
+
class Auth {
|
|
19
|
+
constructor() {
|
|
20
|
+
this.doLogout = (redirectUri = "/") => __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
try {
|
|
22
|
+
const result = yield (0, _fetch_1.default)(`${this.authHost}/api/v1/oauth/logout?client_id=${this.clientId}`, {
|
|
23
|
+
method: "POST"
|
|
24
|
+
});
|
|
25
|
+
const homeUrl = result.data || redirectUri;
|
|
26
|
+
this.doLogin(homeUrl);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
this.doLogin();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
this.userType = "officer";
|
|
33
|
+
this.authHost = "";
|
|
34
|
+
this.clientId = "";
|
|
35
|
+
this.clientSecret = "";
|
|
36
|
+
this.frontendHost = window.location.origin;
|
|
37
|
+
this.defaultCallback = "";
|
|
38
|
+
this.homeUrl = "";
|
|
39
|
+
this.user = { userId: '', email: '' };
|
|
40
|
+
this.queryString = '';
|
|
41
|
+
}
|
|
42
|
+
isPeopleType() {
|
|
43
|
+
return this.userType === "people";
|
|
44
|
+
}
|
|
45
|
+
static getCode() {
|
|
46
|
+
const params = new URLSearchParams(window.location.search);
|
|
47
|
+
return params.get("code") || "";
|
|
48
|
+
}
|
|
49
|
+
setUser(profile) {
|
|
50
|
+
this.user.userId = profile.user_id;
|
|
51
|
+
this.user.email = profile.user_email;
|
|
52
|
+
}
|
|
53
|
+
processInitCookies() {
|
|
54
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
const callbackCode = Auth.getCode();
|
|
56
|
+
if (callbackCode) {
|
|
57
|
+
try {
|
|
58
|
+
yield this.setCookie(callbackCode);
|
|
59
|
+
const params = new URLSearchParams(window.location.search);
|
|
60
|
+
params.delete("code");
|
|
61
|
+
const { pathname } = window.location;
|
|
62
|
+
const replaceURL = params.toString()
|
|
63
|
+
? `${pathname}?${params.toString()}`
|
|
64
|
+
: pathname;
|
|
65
|
+
window.history.replaceState({}, "", replaceURL);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
this.redirectToAuth();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
doLogin(callbackUri = this.defaultCallback) {
|
|
75
|
+
let url = new URL(callbackUri, this.frontendHost).toString();
|
|
76
|
+
if (!callbackUri) {
|
|
77
|
+
url = url.slice(0, url.length - 1);
|
|
78
|
+
}
|
|
79
|
+
const callbackURL = encodeURIComponent(url);
|
|
80
|
+
let target = `${this.authHost}/auth/login?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
81
|
+
if (this.isPeopleType()) {
|
|
82
|
+
target = `${this.authHost}/auth/${PEOPLE_PATH}/login?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
83
|
+
}
|
|
84
|
+
window.location.replace(target);
|
|
85
|
+
}
|
|
86
|
+
doRegister(callbackUri = this.defaultCallback) {
|
|
87
|
+
const url = new URL(callbackUri, this.frontendHost).toString();
|
|
88
|
+
const callbackURL = encodeURIComponent(url);
|
|
89
|
+
let location = `${this.authHost}/auth/register?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
90
|
+
if (this.isPeopleType()) {
|
|
91
|
+
location = `${this.authHost}/auth/people/main-register?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
92
|
+
}
|
|
93
|
+
window.location.replace(location);
|
|
94
|
+
}
|
|
95
|
+
createQueryParam(data) {
|
|
96
|
+
const searchParams = new URLSearchParams();
|
|
97
|
+
for (const key in data) {
|
|
98
|
+
searchParams.append(key, data[key]);
|
|
99
|
+
}
|
|
100
|
+
return searchParams.toString();
|
|
101
|
+
}
|
|
102
|
+
init(options) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
this.userType = options.userType;
|
|
105
|
+
this.authHost = options.authHost;
|
|
106
|
+
this.clientId = options.clientId;
|
|
107
|
+
this.clientSecret = options.clientSecret;
|
|
108
|
+
this.homeUrl = options.homeUrl;
|
|
109
|
+
this.queryString = this.createQueryParam({
|
|
110
|
+
'client_id': this.clientId,
|
|
111
|
+
'user_type': this.userType
|
|
112
|
+
});
|
|
113
|
+
yield this.processInitCookies();
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
doResetPassword() {
|
|
117
|
+
window.location.replace(`${this.authHost}/auth/change-password?redirectUri=${window.location.href}&client_id=${this.clientId}&user_type=${this.userType}`);
|
|
118
|
+
}
|
|
119
|
+
doForgotPassword() {
|
|
120
|
+
window.location.replace(`${this.authHost}/auth/forgot-password?redirectUri=${window.location.href}&client_id=${this.clientId}&user_type=${this.userType}`);
|
|
121
|
+
}
|
|
122
|
+
redirectToAuth() {
|
|
123
|
+
let target = `${this.authHost}/auth/login?redirectUri=${window.location.href}&client_id=${this.clientId}`;
|
|
124
|
+
if (this.isPeopleType()) {
|
|
125
|
+
target = `${this.authHost}/auth/${PEOPLE_PATH}/login?redirectUri=${window.location.href}&client_id=${this.clientId}`;
|
|
126
|
+
}
|
|
127
|
+
window.location.replace(target);
|
|
128
|
+
}
|
|
129
|
+
setCookie(code) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
131
|
+
const codeParam = `&code=${code}`;
|
|
132
|
+
yield (0, _fetch_1.default)(`${this.authHost}/api/v1/oauth/tokenByCode?${this.queryString}${codeParam}`, {
|
|
133
|
+
method: "POST",
|
|
134
|
+
body: JSON.stringify({ code, homeUrl: this.homeUrl })
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
getProfile(isSso = true) {
|
|
139
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
140
|
+
try {
|
|
141
|
+
const secretParam = `&${CLIENT_SECRET}=${this.clientSecret}`;
|
|
142
|
+
let profileEndpoint = isSso ? 'profile_sso' : `profile`;
|
|
143
|
+
const result = yield (0, _fetch_1.default)(`${this.authHost}/api/v1/oauth/${profileEndpoint}?${this.queryString}${secretParam}`);
|
|
144
|
+
const profile = result.data;
|
|
145
|
+
this.setUser(profile);
|
|
146
|
+
return profile;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
console.log('error', error);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const bedRockAuth = new Auth();
|
|
155
|
+
exports.default = bedRockAuth;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getProfile = exports.setCookie = exports.doForgotPassword = exports.doResetPassword = exports.doRegister = exports.doLogout = exports.doLogin = exports.initBedrockAuth = void 0;
|
|
7
|
+
const auth_1 = __importDefault(require("./auth"));
|
|
8
|
+
const initBedrockAuth = ({ authUri, clientId, clientSecret, callback, userType, homeUrl }) => {
|
|
9
|
+
auth_1.default.init({
|
|
10
|
+
authHost: authUri,
|
|
11
|
+
clientId,
|
|
12
|
+
clientSecret,
|
|
13
|
+
userType: userType !== null && userType !== void 0 ? userType : "officer",
|
|
14
|
+
homeUrl: homeUrl
|
|
15
|
+
})
|
|
16
|
+
.then(() => {
|
|
17
|
+
callback();
|
|
18
|
+
})
|
|
19
|
+
.catch((error) => console.log(error));
|
|
20
|
+
};
|
|
21
|
+
exports.initBedrockAuth = initBedrockAuth;
|
|
22
|
+
const doLogin = (callbackUri) => auth_1.default.doLogin(callbackUri);
|
|
23
|
+
exports.doLogin = doLogin;
|
|
24
|
+
const doLogout = (redirectUri) => auth_1.default.doLogout(redirectUri);
|
|
25
|
+
exports.doLogout = doLogout;
|
|
26
|
+
const doRegister = (callbackUri) => auth_1.default.doRegister(callbackUri);
|
|
27
|
+
exports.doRegister = doRegister;
|
|
28
|
+
const doResetPassword = () => auth_1.default.doResetPassword();
|
|
29
|
+
exports.doResetPassword = doResetPassword;
|
|
30
|
+
const doForgotPassword = () => auth_1.default.doForgotPassword();
|
|
31
|
+
exports.doForgotPassword = doForgotPassword;
|
|
32
|
+
const setCookie = (code) => auth_1.default.setCookie(code);
|
|
33
|
+
exports.setCookie = setCookie;
|
|
34
|
+
const getProfile = (isSso) => auth_1.default.getProfile(isSso);
|
|
35
|
+
exports.getProfile = getProfile;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fetch.d.ts","sourceRoot":"","sources":["../../../_fetch.ts"],"names":[],"mappings":"AAQA,UAAU,kBAAmB,SAAQ,WAAW;IAC5C,OAAO,CAAC,EAAE,WAAW,CAAC;CACzB;AAED,iBAAe,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,CAAC,CAAC,CA2BvF;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type UserType = "people" | "officer";
|
|
2
|
+
interface Options {
|
|
3
|
+
authHost: string;
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
userType: UserType;
|
|
7
|
+
homeUrl: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AuthServiceResponse {
|
|
10
|
+
user_id: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface AuthServiceRequest {
|
|
14
|
+
access_token: string;
|
|
15
|
+
refresh_token?: string;
|
|
16
|
+
home_url?: string;
|
|
17
|
+
}
|
|
18
|
+
declare class Auth {
|
|
19
|
+
private userType;
|
|
20
|
+
private authHost;
|
|
21
|
+
private frontendHost;
|
|
22
|
+
private defaultCallback;
|
|
23
|
+
private clientId;
|
|
24
|
+
private clientSecret;
|
|
25
|
+
private homeUrl;
|
|
26
|
+
private user;
|
|
27
|
+
private queryString;
|
|
28
|
+
constructor();
|
|
29
|
+
isPeopleType(): boolean;
|
|
30
|
+
static getCode(): string;
|
|
31
|
+
private setUser;
|
|
32
|
+
private processInitCookies;
|
|
33
|
+
doLogin(callbackUri?: string): void;
|
|
34
|
+
doLogout: (redirectUri?: string) => Promise<void>;
|
|
35
|
+
doRegister(callbackUri?: string): void;
|
|
36
|
+
private createQueryParam;
|
|
37
|
+
init(options: Options): Promise<void>;
|
|
38
|
+
doResetPassword(): void;
|
|
39
|
+
doForgotPassword(): void;
|
|
40
|
+
private redirectToAuth;
|
|
41
|
+
setCookie(code: string): Promise<void>;
|
|
42
|
+
getProfile(isSso?: boolean): Promise<AuthServiceResponse | undefined>;
|
|
43
|
+
}
|
|
44
|
+
declare const bedRockAuth: Auth;
|
|
45
|
+
export default bedRockAuth;
|
|
46
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../auth.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;AAU5C,UAAU,OAAO;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,QAAQ,CAAC;IACnB,OAAO,EAAG,MAAM,CAAC;CAClB;AAQD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAQD,cAAM,IAAI;IACR,OAAO,CAAC,QAAQ,CAAW;IAE3B,OAAO,CAAC,QAAQ,CAAS;IAEzB,OAAO,CAAC,YAAY,CAAS;IAE7B,OAAO,CAAC,eAAe,CAAS;IAEhC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,YAAY,CAAS;IAE7B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,IAAI,CAAQ;IACpB,OAAO,CAAC,WAAW,CAAS;;IAc5B,YAAY;IAIZ,MAAM,CAAC,OAAO;IAKd,OAAO,CAAC,OAAO;YAKD,kBAAkB;IAmBzB,OAAO,CAAC,WAAW,GAAE,MAA6B;IAalD,QAAQ,iBAAuB,MAAM,mBAU1C;IAEK,UAAU,CAAC,WAAW,GAAE,MAA6B;IAW5D,OAAO,CAAC,gBAAgB;IAQX,IAAI,CAAC,OAAO,EAAE,OAAO;IAa3B,eAAe;IAMf,gBAAgB;IAMvB,OAAO,CAAC,cAAc;IAQT,SAAS,CAAC,IAAI,EAAE,MAAM;IAUtB,UAAU,CAAC,KAAK,GAAE,OAAc;CAa9C;AAED,QAAA,MAAM,WAAW,MAAa,CAAC;AAE/B,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { UserType } from "./auth";
|
|
2
|
+
export type AuthProps = {
|
|
3
|
+
userType?: UserType;
|
|
4
|
+
authUri: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
homeUrl: string;
|
|
8
|
+
callback: () => void;
|
|
9
|
+
};
|
|
10
|
+
export declare const initBedrockAuth: ({ authUri, clientId, clientSecret, callback, userType, homeUrl }: AuthProps) => void;
|
|
11
|
+
export declare const doLogin: (callbackUri?: string) => void;
|
|
12
|
+
export declare const doLogout: (redirectUri?: string) => Promise<void>;
|
|
13
|
+
export declare const doRegister: (callbackUri?: string) => void;
|
|
14
|
+
export declare const doResetPassword: () => void;
|
|
15
|
+
export declare const doForgotPassword: () => void;
|
|
16
|
+
export declare const setCookie: (code: string) => Promise<void>;
|
|
17
|
+
export declare const getProfile: (isSso?: boolean) => Promise<import("./auth").AuthServiceResponse | undefined>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../index.ts"],"names":[],"mappings":"AAAA,OAAW,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAEtC,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAG,MAAM,CAAC;IACtB,OAAO,EAAG,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,eAAe,qEAOzB,SAAS,SAYX,CAAC;AAEF,eAAO,MAAM,OAAO,iBAAkB,MAAM,SAA4B,CAAC;AAEzE,eAAO,MAAM,QAAQ,iBAAkB,MAAM,kBAA6B,CAAC;AAE3E,eAAO,MAAM,UAAU,iBAAkB,MAAM,SAA+B,CAAC;AAE/E,eAAO,MAAM,eAAe,YAA6B,CAAC;AAE1D,eAAO,MAAM,gBAAgB,YAA8B,CAAC;AAE5D,eAAO,MAAM,SAAS,SAAW,MAAM,kBAAuB,CAAC;AAE/D,eAAO,MAAM,UAAU,WAAa,OAAO,8DAAyB,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const defaultHeaders = {
|
|
2
|
+
'Content-Type': 'application/json',
|
|
3
|
+
};
|
|
4
|
+
async function customFetch(url, options = {}) {
|
|
5
|
+
const headers = {
|
|
6
|
+
...defaultHeaders,
|
|
7
|
+
...options.headers,
|
|
8
|
+
};
|
|
9
|
+
const requestOptions = {
|
|
10
|
+
...options,
|
|
11
|
+
credentials: 'include',
|
|
12
|
+
mode: "cors"
|
|
13
|
+
};
|
|
14
|
+
requestOptions.headers = headers;
|
|
15
|
+
const response = await fetch(`${url}`, requestOptions);
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
18
|
+
}
|
|
19
|
+
const contentType = response.headers.get('content-type');
|
|
20
|
+
if (contentType && contentType.includes('application/json')) {
|
|
21
|
+
return response.json();
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
return {};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export default customFetch;
|
package/dist/esm/auth.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import customFetch from "./_fetch";
|
|
2
|
+
const PEOPLE_PATH = "people";
|
|
3
|
+
const CLIENT_SECRET = "client_secret";
|
|
4
|
+
class Auth {
|
|
5
|
+
userType;
|
|
6
|
+
authHost;
|
|
7
|
+
frontendHost;
|
|
8
|
+
defaultCallback;
|
|
9
|
+
clientId;
|
|
10
|
+
clientSecret;
|
|
11
|
+
homeUrl;
|
|
12
|
+
user;
|
|
13
|
+
queryString;
|
|
14
|
+
constructor() {
|
|
15
|
+
this.userType = "officer";
|
|
16
|
+
this.authHost = "";
|
|
17
|
+
this.clientId = "";
|
|
18
|
+
this.clientSecret = "";
|
|
19
|
+
this.frontendHost = window.location.origin;
|
|
20
|
+
this.defaultCallback = "";
|
|
21
|
+
this.homeUrl = "";
|
|
22
|
+
this.user = { userId: '', email: '' };
|
|
23
|
+
this.queryString = '';
|
|
24
|
+
}
|
|
25
|
+
isPeopleType() {
|
|
26
|
+
return this.userType === "people";
|
|
27
|
+
}
|
|
28
|
+
static getCode() {
|
|
29
|
+
const params = new URLSearchParams(window.location.search);
|
|
30
|
+
return params.get("code") || "";
|
|
31
|
+
}
|
|
32
|
+
setUser(profile) {
|
|
33
|
+
this.user.userId = profile.user_id;
|
|
34
|
+
this.user.email = profile.user_email;
|
|
35
|
+
}
|
|
36
|
+
async processInitCookies() {
|
|
37
|
+
const callbackCode = Auth.getCode();
|
|
38
|
+
if (callbackCode) {
|
|
39
|
+
try {
|
|
40
|
+
await this.setCookie(callbackCode);
|
|
41
|
+
const params = new URLSearchParams(window.location.search);
|
|
42
|
+
params.delete("code");
|
|
43
|
+
const { pathname } = window.location;
|
|
44
|
+
const replaceURL = params.toString()
|
|
45
|
+
? `${pathname}?${params.toString()}`
|
|
46
|
+
: pathname;
|
|
47
|
+
window.history.replaceState({}, "", replaceURL);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
this.redirectToAuth();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
doLogin(callbackUri = this.defaultCallback) {
|
|
56
|
+
let url = new URL(callbackUri, this.frontendHost).toString();
|
|
57
|
+
if (!callbackUri) {
|
|
58
|
+
url = url.slice(0, url.length - 1);
|
|
59
|
+
}
|
|
60
|
+
const callbackURL = encodeURIComponent(url);
|
|
61
|
+
let target = `${this.authHost}/auth/login?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
62
|
+
if (this.isPeopleType()) {
|
|
63
|
+
target = `${this.authHost}/auth/${PEOPLE_PATH}/login?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
64
|
+
}
|
|
65
|
+
window.location.replace(target);
|
|
66
|
+
}
|
|
67
|
+
doLogout = async (redirectUri = "/") => {
|
|
68
|
+
try {
|
|
69
|
+
const result = await customFetch(`${this.authHost}/api/v1/oauth/logout?client_id=${this.clientId}`, {
|
|
70
|
+
method: "POST"
|
|
71
|
+
});
|
|
72
|
+
const homeUrl = result.data || redirectUri;
|
|
73
|
+
this.doLogin(homeUrl);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
this.doLogin();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
doRegister(callbackUri = this.defaultCallback) {
|
|
80
|
+
const url = new URL(callbackUri, this.frontendHost).toString();
|
|
81
|
+
const callbackURL = encodeURIComponent(url);
|
|
82
|
+
let location = `${this.authHost}/auth/register?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
83
|
+
if (this.isPeopleType()) {
|
|
84
|
+
location = `${this.authHost}/auth/people/main-register?redirectUri=${callbackURL}&client_id=${this.clientId}`;
|
|
85
|
+
}
|
|
86
|
+
window.location.replace(location);
|
|
87
|
+
}
|
|
88
|
+
createQueryParam(data) {
|
|
89
|
+
const searchParams = new URLSearchParams();
|
|
90
|
+
for (const key in data) {
|
|
91
|
+
searchParams.append(key, data[key]);
|
|
92
|
+
}
|
|
93
|
+
return searchParams.toString();
|
|
94
|
+
}
|
|
95
|
+
async init(options) {
|
|
96
|
+
this.userType = options.userType;
|
|
97
|
+
this.authHost = options.authHost;
|
|
98
|
+
this.clientId = options.clientId;
|
|
99
|
+
this.clientSecret = options.clientSecret;
|
|
100
|
+
this.homeUrl = options.homeUrl;
|
|
101
|
+
this.queryString = this.createQueryParam({
|
|
102
|
+
'client_id': this.clientId,
|
|
103
|
+
'user_type': this.userType
|
|
104
|
+
});
|
|
105
|
+
await this.processInitCookies();
|
|
106
|
+
}
|
|
107
|
+
doResetPassword() {
|
|
108
|
+
window.location.replace(`${this.authHost}/auth/change-password?redirectUri=${window.location.href}&client_id=${this.clientId}&user_type=${this.userType}`);
|
|
109
|
+
}
|
|
110
|
+
doForgotPassword() {
|
|
111
|
+
window.location.replace(`${this.authHost}/auth/forgot-password?redirectUri=${window.location.href}&client_id=${this.clientId}&user_type=${this.userType}`);
|
|
112
|
+
}
|
|
113
|
+
redirectToAuth() {
|
|
114
|
+
let target = `${this.authHost}/auth/login?redirectUri=${window.location.href}&client_id=${this.clientId}`;
|
|
115
|
+
if (this.isPeopleType()) {
|
|
116
|
+
target = `${this.authHost}/auth/${PEOPLE_PATH}/login?redirectUri=${window.location.href}&client_id=${this.clientId}`;
|
|
117
|
+
}
|
|
118
|
+
window.location.replace(target);
|
|
119
|
+
}
|
|
120
|
+
async setCookie(code) {
|
|
121
|
+
const codeParam = `&code=${code}`;
|
|
122
|
+
await customFetch(`${this.authHost}/api/v1/oauth/tokenByCode?${this.queryString}${codeParam}`, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: JSON.stringify({ code, homeUrl: this.homeUrl })
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
async getProfile(isSso = true) {
|
|
128
|
+
try {
|
|
129
|
+
const secretParam = `&${CLIENT_SECRET}=${this.clientSecret}`;
|
|
130
|
+
let profileEndpoint = isSso ? 'profile_sso' : `profile`;
|
|
131
|
+
const result = await customFetch(`${this.authHost}/api/v1/oauth/${profileEndpoint}?${this.queryString}${secretParam}`);
|
|
132
|
+
const profile = result.data;
|
|
133
|
+
this.setUser(profile);
|
|
134
|
+
return profile;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
console.log('error', error);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const bedRockAuth = new Auth();
|
|
142
|
+
export default bedRockAuth;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import BR from "./auth";
|
|
2
|
+
export const initBedrockAuth = ({ authUri, clientId, clientSecret, callback, userType, homeUrl }) => {
|
|
3
|
+
BR.init({
|
|
4
|
+
authHost: authUri,
|
|
5
|
+
clientId,
|
|
6
|
+
clientSecret,
|
|
7
|
+
userType: userType ?? "officer",
|
|
8
|
+
homeUrl: homeUrl
|
|
9
|
+
})
|
|
10
|
+
.then(() => {
|
|
11
|
+
callback();
|
|
12
|
+
})
|
|
13
|
+
.catch((error) => console.log(error));
|
|
14
|
+
};
|
|
15
|
+
export const doLogin = (callbackUri) => BR.doLogin(callbackUri);
|
|
16
|
+
export const doLogout = (redirectUri) => BR.doLogout(redirectUri);
|
|
17
|
+
export const doRegister = (callbackUri) => BR.doRegister(callbackUri);
|
|
18
|
+
export const doResetPassword = () => BR.doResetPassword();
|
|
19
|
+
export const doForgotPassword = () => BR.doForgotPassword();
|
|
20
|
+
export const setCookie = (code) => BR.setCookie(code);
|
|
21
|
+
export const getProfile = (isSso) => BR.getProfile(isSso);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_fetch.d.ts","sourceRoot":"","sources":["../../../_fetch.ts"],"names":[],"mappings":"AAQA,UAAU,kBAAmB,SAAQ,WAAW;IAC5C,OAAO,CAAC,EAAE,WAAW,CAAC;CACzB;AAED,iBAAe,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,kBAAuB,GAAG,OAAO,CAAC,CAAC,CAAC,CA2BvF;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export type UserType = "people" | "officer";
|
|
2
|
+
interface Options {
|
|
3
|
+
authHost: string;
|
|
4
|
+
clientId: string;
|
|
5
|
+
clientSecret: string;
|
|
6
|
+
userType: UserType;
|
|
7
|
+
homeUrl: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AuthServiceResponse {
|
|
10
|
+
user_id: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface AuthServiceRequest {
|
|
14
|
+
access_token: string;
|
|
15
|
+
refresh_token?: string;
|
|
16
|
+
home_url?: string;
|
|
17
|
+
}
|
|
18
|
+
declare class Auth {
|
|
19
|
+
private userType;
|
|
20
|
+
private authHost;
|
|
21
|
+
private frontendHost;
|
|
22
|
+
private defaultCallback;
|
|
23
|
+
private clientId;
|
|
24
|
+
private clientSecret;
|
|
25
|
+
private homeUrl;
|
|
26
|
+
private user;
|
|
27
|
+
private queryString;
|
|
28
|
+
constructor();
|
|
29
|
+
isPeopleType(): boolean;
|
|
30
|
+
static getCode(): string;
|
|
31
|
+
private setUser;
|
|
32
|
+
private processInitCookies;
|
|
33
|
+
doLogin(callbackUri?: string): void;
|
|
34
|
+
doLogout: (redirectUri?: string) => Promise<void>;
|
|
35
|
+
doRegister(callbackUri?: string): void;
|
|
36
|
+
private createQueryParam;
|
|
37
|
+
init(options: Options): Promise<void>;
|
|
38
|
+
doResetPassword(): void;
|
|
39
|
+
doForgotPassword(): void;
|
|
40
|
+
private redirectToAuth;
|
|
41
|
+
setCookie(code: string): Promise<void>;
|
|
42
|
+
getProfile(isSso?: boolean): Promise<AuthServiceResponse | undefined>;
|
|
43
|
+
}
|
|
44
|
+
declare const bedRockAuth: Auth;
|
|
45
|
+
export default bedRockAuth;
|
|
46
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../../auth.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;AAU5C,UAAU,OAAO;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,QAAQ,CAAC;IACnB,OAAO,EAAG,MAAM,CAAC;CAClB;AAQD,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAQD,cAAM,IAAI;IACR,OAAO,CAAC,QAAQ,CAAW;IAE3B,OAAO,CAAC,QAAQ,CAAS;IAEzB,OAAO,CAAC,YAAY,CAAS;IAE7B,OAAO,CAAC,eAAe,CAAS;IAEhC,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,YAAY,CAAS;IAE7B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,IAAI,CAAQ;IACpB,OAAO,CAAC,WAAW,CAAS;;IAc5B,YAAY;IAIZ,MAAM,CAAC,OAAO;IAKd,OAAO,CAAC,OAAO;YAKD,kBAAkB;IAmBzB,OAAO,CAAC,WAAW,GAAE,MAA6B;IAalD,QAAQ,iBAAuB,MAAM,mBAU1C;IAEK,UAAU,CAAC,WAAW,GAAE,MAA6B;IAW5D,OAAO,CAAC,gBAAgB;IAQX,IAAI,CAAC,OAAO,EAAE,OAAO;IAa3B,eAAe;IAMf,gBAAgB;IAMvB,OAAO,CAAC,cAAc;IAQT,SAAS,CAAC,IAAI,EAAE,MAAM;IAUtB,UAAU,CAAC,KAAK,GAAE,OAAc;CAa9C;AAED,QAAA,MAAM,WAAW,MAAa,CAAC;AAE/B,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { UserType } from "./auth";
|
|
2
|
+
export type AuthProps = {
|
|
3
|
+
userType?: UserType;
|
|
4
|
+
authUri: string;
|
|
5
|
+
clientId: string;
|
|
6
|
+
clientSecret: string;
|
|
7
|
+
homeUrl: string;
|
|
8
|
+
callback: () => void;
|
|
9
|
+
};
|
|
10
|
+
export declare const initBedrockAuth: ({ authUri, clientId, clientSecret, callback, userType, homeUrl }: AuthProps) => void;
|
|
11
|
+
export declare const doLogin: (callbackUri?: string) => void;
|
|
12
|
+
export declare const doLogout: (redirectUri?: string) => Promise<void>;
|
|
13
|
+
export declare const doRegister: (callbackUri?: string) => void;
|
|
14
|
+
export declare const doResetPassword: () => void;
|
|
15
|
+
export declare const doForgotPassword: () => void;
|
|
16
|
+
export declare const setCookie: (code: string) => Promise<void>;
|
|
17
|
+
export declare const getProfile: (isSso?: boolean) => Promise<import("./auth").AuthServiceResponse | undefined>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../index.ts"],"names":[],"mappings":"AAAA,OAAW,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAEtC,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAG,MAAM,CAAC;IACtB,OAAO,EAAG,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,eAAe,qEAOzB,SAAS,SAYX,CAAC;AAEF,eAAO,MAAM,OAAO,iBAAkB,MAAM,SAA4B,CAAC;AAEzE,eAAO,MAAM,QAAQ,iBAAkB,MAAM,kBAA6B,CAAC;AAE3E,eAAO,MAAM,UAAU,iBAAkB,MAAM,SAA+B,CAAC;AAE/E,eAAO,MAAM,eAAe,YAA6B,CAAC;AAE1D,eAAO,MAAM,gBAAgB,YAA8B,CAAC;AAE5D,eAAO,MAAM,SAAS,SAAW,MAAM,kBAAuB,CAAC;AAE/D,eAAO,MAAM,UAAU,WAAa,OAAO,8DAAyB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arv-bedrock/auth-sso",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "./dist/cjs/index.js",
|
|
5
|
+
"types": "./dist/cjs/types/index.d.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"clean": "rm -rf ./dist",
|
|
8
|
+
"build": "npm run clean && npm run build:esm && npm run build:cjs",
|
|
9
|
+
"build:esm": "tsc -p ./configs/tsconfig.esm.json && mv dist/esm/index.js dist/esm/index.mjs",
|
|
10
|
+
"build:cjs": "tsc -p ./configs/tsconfig.cjs.json",
|
|
11
|
+
"prepack": "npm run build",
|
|
12
|
+
"prepublish": "npm run build",
|
|
13
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
14
|
+
"semantic-release": "semantic-release"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [],
|
|
17
|
+
"author": "",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"axios": "^1.1.3",
|
|
21
|
+
"typescript": "4.9.4"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./dist/esm/types/index.d.ts",
|
|
27
|
+
"default": "./dist/esm/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"require": {
|
|
30
|
+
"types": "./dist/cjs/types/index.d.ts",
|
|
31
|
+
"default": "./dist/cjs/index.js"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist/**/*"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@semantic-release/changelog": "^6.0.2",
|
|
40
|
+
"@semantic-release/git": "^10.0.1",
|
|
41
|
+
"@semantic-release/gitlab": "^9.5.1",
|
|
42
|
+
"@semantic-release/npm": "^9.0.1",
|
|
43
|
+
"@types/node": "^18.11.9",
|
|
44
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
45
|
+
"semantic-release": "^20.0.2",
|
|
46
|
+
"tcs": "^10.0.2"
|
|
47
|
+
}
|
|
48
|
+
}
|