@cocreate/authenticate 1.0.1 → 1.0.3

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/src/index.js CHANGED
@@ -1,102 +1,102 @@
1
- const jwt = require("jsonwebtoken")
2
- const uid = require("@cocreate/uuid")
3
-
4
- class CoCreateAuthenticate {
5
- /**
6
- * config structure
7
- * https://www.npmjs.com/package/jsonwebtoken
8
- {
9
- key: 'xxxxxx', // any value
10
- options: {
11
- algorithm: "HS256",
12
- expiresIn: "30m",
13
- issuer: "issuer"
14
- }
15
- }
16
- **/
17
-
18
-
19
- constructor(config) {
20
- this.config = config
21
- this.config['key'] = uid.generate(40)
22
- }
23
-
24
- async generateToken({ user_id }) {
25
- try {
26
- const { key, options } = this.config
27
- const result = {
28
- token: jwt.sign({ user_id }, key, options),
29
- }
30
- return result.token;
31
- } catch (err) {
32
- return null
33
- }
34
- }
35
-
36
- async getUserId(req) {
37
- try {
38
- let { user_id } = await this.wsCheck(req)
39
- return user_id
40
- } catch (err) {
41
- return null
42
- }
43
- }
44
-
45
- getTokenFromCookie(cookie) {
46
- let token = null;
47
- if (cookie) {
48
- cookie.split(';').forEach((c) => {
49
- try {
50
- var parts = c.split('=')
51
- if (parts[0].trim() == 'token') {
52
- token = decodeURI(parts[1].trim());
53
- }
54
- } catch (err) {
55
- console.log(err)
56
- }
57
- })
58
- }
59
- return token;
60
- }
61
-
62
- async wsCheck(req) {
63
- const headers = req.headers
64
- let token = headers['sec-websocket-protocol'];
65
- // let token = this.getTokenFromCookie(headers.cookie);
66
- // if (!token) {
67
- // token = headers['sec-websocket-protocol'];
68
- // }
69
-
70
- let result = null;
71
- if (token && token !== 'null') {
72
- result = await this.verifiyToken(token);
73
- }
74
-
75
- return result;
76
- }
77
-
78
- async httpCheck(req) {
79
-
80
- }
81
-
82
- async verifiyToken(token) {
83
- try {
84
- let decoded = await jwt.verify(token, this.config.key)
85
- return decoded;
86
- } catch (err) {
87
- if (err.message === 'jwt expired') {
88
- console.log('Expired Token')
89
- return null
90
- } else if (err.message === 'invalid token') {
91
- console.log('Invalid Token')
92
- return null
93
- } else {
94
- console.log('Invalid token', token)
95
- return null;
96
- }
97
- }
98
-
99
- }
100
- }
101
-
1
+ const jwt = require("jsonwebtoken")
2
+ const uid = require("@cocreate/uuid")
3
+
4
+ class CoCreateAuthenticate {
5
+ /**
6
+ * config structure
7
+ * https://www.npmjs.com/package/jsonwebtoken
8
+ {
9
+ key: 'xxxxxx', // any value
10
+ options: {
11
+ algorithm: "HS256",
12
+ expiresIn: "30m",
13
+ issuer: "issuer"
14
+ }
15
+ }
16
+ **/
17
+
18
+
19
+ constructor(config) {
20
+ this.config = config
21
+ this.config['key'] = uid.generate(40)
22
+ }
23
+
24
+ async generateToken({ user_id }) {
25
+ try {
26
+ const { key, options } = this.config
27
+ const result = {
28
+ token: jwt.sign({ user_id }, key, options),
29
+ }
30
+ return result.token;
31
+ } catch (err) {
32
+ return null
33
+ }
34
+ }
35
+
36
+ async getUserId(req) {
37
+ try {
38
+ let { user_id } = await this.wsCheck(req)
39
+ return user_id
40
+ } catch (err) {
41
+ return null
42
+ }
43
+ }
44
+
45
+ getTokenFromCookie(cookie) {
46
+ let token = null;
47
+ if (cookie) {
48
+ cookie.split(';').forEach((c) => {
49
+ try {
50
+ var parts = c.split('=')
51
+ if (parts[0].trim() == 'token') {
52
+ token = decodeURI(parts[1].trim());
53
+ }
54
+ } catch (err) {
55
+ console.log(err)
56
+ }
57
+ })
58
+ }
59
+ return token;
60
+ }
61
+
62
+ async wsCheck(req) {
63
+ const headers = req.headers
64
+ let token = headers['sec-websocket-protocol'];
65
+ // let token = this.getTokenFromCookie(headers.cookie);
66
+ // if (!token) {
67
+ // token = headers['sec-websocket-protocol'];
68
+ // }
69
+
70
+ let result = null;
71
+ if (token && token !== 'null') {
72
+ result = await this.verifiyToken(token);
73
+ }
74
+
75
+ return result;
76
+ }
77
+
78
+ async httpCheck(req) {
79
+
80
+ }
81
+
82
+ async verifiyToken(token) {
83
+ try {
84
+ let decoded = await jwt.verify(token, this.config.key)
85
+ return decoded;
86
+ } catch (err) {
87
+ if (err.message === 'jwt expired') {
88
+ console.log('Expired Token')
89
+ return null
90
+ } else if (err.message === 'invalid token') {
91
+ console.log('Invalid Token')
92
+ return null
93
+ } else {
94
+ console.log('Invalid token', token)
95
+ return null;
96
+ }
97
+ }
98
+
99
+ }
100
+ }
101
+
102
102
  module.exports = CoCreateAuthenticate;