authscape 1.0.122 → 1.0.123

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.
@@ -1,3 +1,4 @@
1
+ "use client"
1
2
  import React, { Component } from 'react';
2
3
  //import {apiService} from 'authscape';
3
4
  import DataTable, {createTheme} from "react-data-table-component";
@@ -1,3 +1,5 @@
1
+ "use client"
2
+
1
3
  import React, { useState, useRef, useEffect } from "react";
2
4
  import { apiService } from "../services/authService";
3
5
  import LinearProgress from "@mui/material/LinearProgress";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authscape",
3
- "version": "1.0.122",
3
+ "version": "1.0.123",
4
4
  "scripts": {
5
5
  "dev": "next dev",
6
6
  "build": "next build",
@@ -8,6 +8,11 @@
8
8
  "lint": "next lint"
9
9
  },
10
10
  "dependencies": {
11
+ "@emotion/react": "^11.11.0",
12
+ "@emotion/styled": "^11.11.0",
13
+ "@mui/icons-material": "^5.11.16",
14
+ "@mui/material": "^5.13.2",
15
+ "@mui/styled-engine-sc": "^5.12.0",
11
16
  "@stripe/react-stripe-js": "^2.1.0",
12
17
  "@stripe/stripe-js": "^1.53.0",
13
18
  "axios": "^1.4.0",
@@ -17,6 +22,7 @@
17
22
  "query-string": "^8.1.0",
18
23
  "react": "18.2.0",
19
24
  "react-data-table-component": "^7.5.3",
20
- "react-dom": "18.2.0"
25
+ "react-dom": "18.2.0",
26
+ "styled-components": "^5.3.11"
21
27
  }
22
28
  }
@@ -0,0 +1,253 @@
1
+ import axios from 'axios'
2
+ import querystring from 'query-string';
3
+ import fileDownload from 'js-file-download';
4
+ import { parseCookies, setCookie, destroyCookie } from 'nookies';
5
+
6
+ const setupDefaultOptions = async (ctx = null) => {
7
+
8
+ let defaultOptions = {};
9
+ if (ctx == null)
10
+ {
11
+ let accessToken = parseCookies().access_token || '';
12
+
13
+ if (accessToken !== null && accessToken !== undefined && accessToken != "") {
14
+ defaultOptions = {
15
+ headers: {
16
+ Authorization: "Bearer " + accessToken
17
+ }
18
+ };
19
+ }
20
+ else {
21
+ defaultOptions = {
22
+ headers: {
23
+ },
24
+ };
25
+ }
26
+ }
27
+ else
28
+ {
29
+ defaultOptions = {
30
+ headers: {
31
+ },
32
+ };
33
+ }
34
+
35
+ return defaultOptions;
36
+ }
37
+
38
+ const RefreshToken = async (originalRequest, instance) => {
39
+
40
+ let accessToken = parseCookies().access_token || '';
41
+ let refreshToken = parseCookies().refresh_token || '';
42
+
43
+ let response = await instance.post(process.env.AUTHORITYURI + "/connect/token",
44
+ querystring.stringify({
45
+ grant_type: 'refresh_token',
46
+ client_id: process.env.client_id,
47
+ client_secret: process.env.client_secret,
48
+ refresh_token: refreshToken
49
+ }), {
50
+ headers: {
51
+ "Content-Type": "application/x-www-form-urlencoded",
52
+ "Authorization": "Bearer " + accessToken
53
+ }
54
+ });
55
+
56
+ if (response != null && response.status == 200)
57
+ {
58
+ originalRequest.headers['Authorization'] = 'Bearer ' + response.data.access_token;
59
+
60
+ await setCookie(null, "access_token", response.data.access_token,
61
+ {
62
+ maxAge: 2147483647,
63
+ path: '/',
64
+ domain: process.env.cookieDomain,
65
+ secure: true
66
+ });
67
+
68
+ await setCookie(null, "expires_in", response.data.expires_in,
69
+ {
70
+ maxAge: 2147483647,
71
+ path: '/',
72
+ domain: process.env.cookieDomain,
73
+ secure: true
74
+ });
75
+
76
+ await setCookie(null, "refresh_token", response.data.refresh_token,
77
+ {
78
+ maxAge: 2147483647,
79
+ path: '/',
80
+ domain: process.env.cookieDomain,
81
+ secure: true
82
+ });
83
+ }
84
+ }
85
+
86
+ export const apiService = (ctx = null) => {
87
+
88
+ let env = process.env.STAGE;
89
+ if (env == "development")
90
+ {
91
+ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
92
+ }
93
+
94
+ let baseUri = process.env.APIURI + "/api";
95
+
96
+ const instance = axios.create({
97
+ baseURL: baseUri,
98
+ //timeout: 10000,
99
+ params: {} // do not remove this, its added to add params later in the config
100
+ });
101
+
102
+ instance.interceptors.response.use(
103
+ (response) => {
104
+
105
+ return response;
106
+ },
107
+ async (error) => {
108
+ const originalConfig = error.config;
109
+ if (error.response) {
110
+
111
+ if (error.response.status === 401 && !originalConfig._retry) {
112
+ originalConfig._retry = true;
113
+
114
+ // Do something, call refreshToken() request for example;
115
+ await RefreshToken(originalConfig, instance);
116
+
117
+ // return a request
118
+ return instance.request(originalConfig);
119
+ }
120
+
121
+ if (error.response.status === 400) {
122
+ // Do something
123
+
124
+ if (error.response.config.url.includes("/connect/token")) // remove the access and refresh if invalid
125
+ {
126
+ destroyCookie(null, "access_token", {
127
+ maxAge: 2147483647,
128
+ path: '/',
129
+ domain: process.env.cookieDomain
130
+ });
131
+
132
+ destroyCookie(null, "refresh_token", {
133
+ maxAge: 2147483647,
134
+ path: '/',
135
+ domain: process.env.cookieDomain
136
+ });
137
+
138
+ destroyCookie(null, "expires_in", {
139
+ maxAge: 2147483647,
140
+ path: '/',
141
+ domain: process.env.cookieDomain
142
+ });
143
+ }
144
+
145
+ return Promise.reject(error);
146
+ }
147
+ }
148
+
149
+ return Promise.reject(error);
150
+ }
151
+ );
152
+
153
+ return {
154
+
155
+ get: async (url, options= {}) => {
156
+
157
+ try
158
+ {
159
+ let defaultOptions = await setupDefaultOptions(ctx);
160
+ return await instance.get(url, { ...defaultOptions, ...options });
161
+ }
162
+ catch(error)
163
+ {
164
+ return error.response;
165
+ }
166
+ },
167
+ post: async (url, data, options = {}) => {
168
+
169
+ try
170
+ {
171
+ let defaultOptions = await setupDefaultOptions(ctx);
172
+ return await instance.post(url, data, { ...defaultOptions, ...options });
173
+ }
174
+ catch(error)
175
+ {
176
+ return error.response;
177
+ }
178
+ },
179
+ put: async (url, data, options = {}) => {
180
+
181
+ try
182
+ {
183
+ let defaultOptions = await setupDefaultOptions(ctx);
184
+ return await instance.put(url, data, { ...defaultOptions, ...options });
185
+ }
186
+ catch(error)
187
+ {
188
+ return error.response;
189
+ }
190
+ },
191
+ delete: async (url, options = {}) => {
192
+
193
+ try
194
+ {
195
+ let defaultOptions = await setupDefaultOptions(ctx);
196
+ return await instance.delete(url, { ...defaultOptions, ...options });
197
+ }
198
+ catch(error)
199
+ {
200
+ return error.response;
201
+ }
202
+ },
203
+ GetCurrentUser: async () => {
204
+
205
+ try
206
+ {
207
+ let accessToken = parseCookies().access_token || null;
208
+
209
+ if (accessToken)
210
+ {
211
+ let defaultOptions = await setupDefaultOptions(null);
212
+ const response = await instance.get('/UserManagement', defaultOptions);
213
+ if (response != null && response.status == 200)
214
+ {
215
+ return response.data;
216
+ }
217
+ // else if (response != null && response.status == 401)
218
+ // {
219
+ // // call the login window maybe?
220
+ // }
221
+ }
222
+
223
+ } catch(exp) {
224
+ //return -1;
225
+ console.log(exp.message);
226
+ }
227
+ return null;
228
+ },
229
+ DownloadFile: async (url, fileName, completed) => {
230
+
231
+ try
232
+ {
233
+ //let defaultOptions = await setupDefaultOptions();
234
+ let defaultOptions = {};
235
+ let options = { responseType: "blob" };
236
+ let response = await instance.get(url, { ...defaultOptions, ...options });
237
+ if (response.status === 200) {
238
+ fileDownload(response.data, fileName);
239
+ if (completed !== undefined) {
240
+ completed();
241
+ }
242
+ }
243
+ }
244
+ catch(error)
245
+ {
246
+ console.error(error);
247
+ if (completed !== undefined) {
248
+ completed();
249
+ }
250
+ }
251
+ }
252
+ }
253
+ }
@@ -0,0 +1,115 @@
1
+ import React from 'react';
2
+ import { destroyCookie } from 'nookies';
3
+
4
+ export const authService = () => {
5
+
6
+ return {
7
+
8
+ dec2hex: (dec) => {
9
+ return ('0' + dec.toString(16)).substr(-2)
10
+ },
11
+ generateRandomString: () => {
12
+ var array = new Uint32Array(56/2);
13
+ window.crypto.getRandomValues(array);
14
+ return Array.from(array, authService().dec2hex).join('');
15
+ },
16
+ sha256: (plain) => {
17
+ const encoder = new TextEncoder();
18
+ const data = encoder.encode(plain);
19
+ return window.crypto.subtle.digest('SHA-256', data);
20
+ },
21
+ base64urlencode: (a) => {
22
+ var str = "";
23
+ var bytes = new Uint8Array(a);
24
+ var len = bytes.byteLength;
25
+ for (var i = 0; i < len; i++) {
26
+ str += String.fromCharCode(bytes[i]);
27
+ }
28
+ return btoa(str)
29
+ .replace(/\+/g, "-")
30
+ .replace(/\//g, "_")
31
+ .replace(/=+$/, "");
32
+ },
33
+ challenge_from_verifier: async (v) => {
34
+ let hashed = await authService().sha256(v);
35
+ let base64encoded = authService().base64urlencode(hashed);
36
+ return base64encoded;
37
+ },
38
+ login: async (redirectUserUri = null, dnsRecord = null, deviceId = null) => {
39
+
40
+ let state = "1234";
41
+ if (redirectUserUri != null)
42
+ {
43
+ localStorage.setItem("redirectUri", redirectUserUri);
44
+ }
45
+
46
+ let verifier = authService().generateRandomString();
47
+ var challenge = await authService().challenge_from_verifier(verifier);
48
+
49
+ window.localStorage.setItem("verifier", verifier);
50
+
51
+ let redirectUri = window.location.origin + "/signin-oidc";
52
+ let loginUri = process.env.AUTHORITYURI + "/connect/authorize?response_type=code&state=" + state + "&client_id=" + process.env.client_id + "&scope=email%20openid%20offline_access%20profile%20api1&redirect_uri=" + redirectUri + "&code_challenge=" + challenge + "&code_challenge_method=S256";
53
+
54
+ if (deviceId)
55
+ {
56
+ loginUri += "&deviceId=" + deviceId; // will be for chrome extention and mobile apps later
57
+ }
58
+
59
+ window.location.href = loginUri;
60
+ },
61
+ signUp: (redirectUrl = null) => {
62
+
63
+ let AuthUri = process.env.AUTHORITYURI;
64
+
65
+ let url = "";
66
+ if (redirectUrl == null)
67
+ {
68
+ url = AuthUri + "/Account/Register?returnUrl=" + window.location.href;
69
+ localStorage.setItem("redirectUri", window.location.href);
70
+ }
71
+ else
72
+ {
73
+ url = AuthUri + "/Account/Register?returnUrl=" + redirectUrl;
74
+ localStorage.setItem("redirectUri", redirectUrl);
75
+ }
76
+
77
+ window.location.href = url;
78
+ },
79
+ logout: async (redirectUri = null) => {
80
+
81
+ let AuthUri = process.env.AUTHORITYURI;
82
+ let cookieDomain = process.env.cookieDomain;
83
+
84
+ destroyCookie({}, "access_token", {
85
+ maxAge: 2147483647,
86
+ path: '/',
87
+ domain: cookieDomain
88
+ });
89
+
90
+ destroyCookie({}, "refresh_token", {
91
+ maxAge: 2147483647,
92
+ path: '/',
93
+ domain: cookieDomain
94
+ });
95
+
96
+ destroyCookie({}, "expires_in", {
97
+ maxAge: 2147483647,
98
+ path: '/',
99
+ domain: cookieDomain
100
+ });
101
+
102
+ setTimeout(() => {
103
+ if (redirectUri == null)
104
+ {
105
+ window.location.href = AuthUri + "/connect/logout?redirect=" + window.location.href;
106
+ }
107
+ else
108
+ {
109
+ window.location.href = AuthUri + "/connect/logout?redirect=" + redirectUri;
110
+ }
111
+ }, 500);
112
+
113
+ },
114
+ }
115
+ }
@@ -0,0 +1,34 @@
1
+ import React, { useEffect, useState, useRef } from 'react';
2
+ //import apiService from './apiService';
3
+
4
+ export function AuthorizationComponent({children, setCurrentUser, userLoaded, isLoading}) {
5
+
6
+ const [loaded, setLoaded] = useState(false);
7
+ const validateUserSignedIn = async () => {
8
+
9
+ setLoaded(true);
10
+
11
+ let usr = await apiService().GetCurrentUser();
12
+ if (usr != null)
13
+ {
14
+ setCurrentUser(usr);
15
+ }
16
+ else
17
+ {
18
+ setCurrentUser(null);
19
+ }
20
+
21
+ userLoaded();
22
+ }
23
+
24
+ useEffect(() => {
25
+
26
+ if (!loaded)
27
+ {
28
+ validateUserSignedIn();
29
+ }
30
+
31
+ }, [loaded]);
32
+
33
+ return (children)
34
+ }
@@ -0,0 +1,66 @@
1
+ import React, { useEffect, useState } from 'react';
2
+ import axios from 'axios';
3
+ import querystring from "query-string";
4
+ import { setCookie } from 'nookies';
5
+
6
+ export const signInValidator = async (queryCode) => {
7
+
8
+ let codeVerifier = window.localStorage.getItem("verifier");
9
+ if (queryCode != null && codeVerifier != null)
10
+ {
11
+ const headers = {'Content-Type': 'application/x-www-form-urlencoded'}
12
+
13
+ let queryString = querystring.stringify({
14
+ code: queryCode,
15
+ grant_type: "authorization_code",
16
+ redirect_uri: window.location.origin + "/signin-oidc",
17
+ client_id: process.env.client_id,
18
+ client_secret: process.env.client_secret,
19
+ code_verifier: codeVerifier
20
+ });
21
+
22
+ let response = await axios.post(process.env.AUTHORITYURI + '/connect/token', queryString, {
23
+ headers: headers
24
+ });
25
+
26
+ window.localStorage.removeItem("verifier");
27
+
28
+ let domain = process.env.cookieDomain;
29
+
30
+ await setCookie(null, "access_token", response.data.access_token,
31
+ {
32
+ maxAge: 2147483647,
33
+ path: '/',
34
+ domain: domain,
35
+ secure: true
36
+ });
37
+
38
+ await setCookie(null, "expires_in", response.data.expires_in,
39
+ {
40
+ maxAge: 2147483647,
41
+ path: '/',
42
+ domain: domain,
43
+ secure: true
44
+ });
45
+
46
+ await setCookie(null, "refresh_token", response.data.refresh_token,
47
+ {
48
+ maxAge: 2147483647,
49
+ path: '/',
50
+ domain: domain,
51
+ secure: true
52
+ });
53
+
54
+
55
+ let redirectUri = localStorage.getItem("redirectUri")
56
+ localStorage.clear();
57
+ if (redirectUri != null)
58
+ {
59
+ window.location.href = redirectUri;
60
+ }
61
+ else
62
+ {
63
+ window.location.href = "/";
64
+ }
65
+ }
66
+ }
@@ -0,0 +1,11 @@
1
+ export const Slug = (slug) => {
2
+
3
+ let index = slug.lastIndexOf("-") + 1;
4
+ if (slug.length > index)
5
+ {
6
+ slug = slug.substr(index);
7
+ return slug;
8
+ }
9
+
10
+ return null;
11
+ };
@@ -0,0 +1,28 @@
1
+ export const storeWithExpiry = () => {
2
+
3
+ return {
4
+ set: (key, value, ttl) => {
5
+
6
+ const now = new Date()
7
+ const item = {
8
+ value: value,
9
+ expiry: now.getTime() + ttl,
10
+ }
11
+ localStorage.setItem(key, JSON.stringify(item))
12
+ },
13
+ get: (key) => {
14
+
15
+ const itemStr = localStorage.getItem(key)
16
+ if (!itemStr) {
17
+ return null
18
+ }
19
+ const item = JSON.parse(itemStr)
20
+ const now = new Date()
21
+ if (now.getTime() > item.expiry) {
22
+ localStorage.removeItem(key)
23
+ return null
24
+ }
25
+ return item.value
26
+ }
27
+ }
28
+ }