@x-sls/google-auth 1.0.1 → 1.0.2
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/package.json +1 -1
- package/src/client.js +47 -50
- package/src/handlers.js +84 -63
- package/src/index.js +2 -2
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -1,63 +1,60 @@
|
|
|
1
|
-
|
|
1
|
+
const createAuthUrl = ({ GOOGLE_CLIENT_ID, REDIRECT_URI, state = {} }) =>
|
|
2
|
+
`https://accounts.google.com/o/oauth2/v2/auth?client_id=${GOOGLE_CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=profile+email&state=${encodeURIComponent(JSON.stringify(state))}`
|
|
3
|
+
|
|
4
|
+
const getAccessToken = async ({
|
|
2
5
|
GOOGLE_CLIENT_ID,
|
|
3
6
|
GOOGLE_CLIENT_SECRET,
|
|
4
|
-
REDIRECT_URI
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
code,
|
|
21
|
-
redirect_uri: REDIRECT_URI,
|
|
22
|
-
grant_type: 'authorization_code'
|
|
23
|
-
})
|
|
7
|
+
REDIRECT_URI,
|
|
8
|
+
code
|
|
9
|
+
}) => {
|
|
10
|
+
let text
|
|
11
|
+
try {
|
|
12
|
+
const res = await fetch('https://oauth2.googleapis.com/token', {
|
|
13
|
+
method: 'POST',
|
|
14
|
+
headers: {
|
|
15
|
+
'Content-Type': 'application/json'
|
|
16
|
+
},
|
|
17
|
+
body: JSON.stringify({
|
|
18
|
+
client_id: GOOGLE_CLIENT_ID,
|
|
19
|
+
client_secret: GOOGLE_CLIENT_SECRET,
|
|
20
|
+
code,
|
|
21
|
+
redirect_uri: REDIRECT_URI,
|
|
22
|
+
grant_type: 'authorization_code'
|
|
24
23
|
})
|
|
24
|
+
})
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
text = await res.text()
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
throw error
|
|
28
|
+
return JSON.parse(text)
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (text) {
|
|
31
|
+
console.error({ text })
|
|
34
32
|
}
|
|
33
|
+
throw error
|
|
35
34
|
}
|
|
35
|
+
}
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
text = await res.text()
|
|
47
|
-
return JSON.parse(text)
|
|
48
|
-
} catch (error) {
|
|
49
|
-
if (text) {
|
|
50
|
-
console.error({ text })
|
|
37
|
+
const getUserInfo = async ({ access_token }) => {
|
|
38
|
+
let text
|
|
39
|
+
try {
|
|
40
|
+
const res = await fetch('https://www.googleapis.com/oauth2/v1/userinfo', {
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Bearer ${access_token}`
|
|
51
43
|
}
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
}
|
|
44
|
+
})
|
|
55
45
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
46
|
+
text = await res.text()
|
|
47
|
+
return JSON.parse(text)
|
|
48
|
+
} catch (error) {
|
|
49
|
+
if (text) {
|
|
50
|
+
console.error({ text })
|
|
51
|
+
}
|
|
52
|
+
throw error
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
55
|
|
|
63
|
-
module.exports =
|
|
56
|
+
module.exports = {
|
|
57
|
+
createAuthUrl,
|
|
58
|
+
getAccessToken,
|
|
59
|
+
getUserInfo
|
|
60
|
+
}
|
package/src/handlers.js
CHANGED
|
@@ -1,82 +1,103 @@
|
|
|
1
1
|
const jwt = require('jsonwebtoken')
|
|
2
|
+
const { createAuthUrl, getAccessToken, getUserInfo } = require('./client')
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
function initiate({ GOOGLE_CLIENT_ID, REDIRECT_URI }) {
|
|
5
|
+
return event => {
|
|
6
|
+
const { returnUrl } = event?.queryStringParameters || {}
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const state = { returnUrl }
|
|
9
|
+
return {
|
|
10
|
+
statusCode: 302,
|
|
11
|
+
headers: {
|
|
12
|
+
Location: createAuthUrl({
|
|
13
|
+
GOOGLE_CLIENT_ID,
|
|
14
|
+
REDIRECT_URI,
|
|
15
|
+
state
|
|
16
|
+
})
|
|
17
|
+
}
|
|
10
18
|
}
|
|
11
19
|
}
|
|
12
20
|
}
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
function callback({
|
|
23
|
+
GOOGLE_CLIENT_ID,
|
|
24
|
+
GOOGLE_CLIENT_SECRET,
|
|
25
|
+
REDIRECT_URI,
|
|
26
|
+
JWT_SECRET
|
|
27
|
+
}) {
|
|
28
|
+
return async event => {
|
|
29
|
+
const { code, state } = event.queryStringParameters
|
|
21
30
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
let returnUrl
|
|
32
|
+
try {
|
|
33
|
+
const data = JSON.parse(state)
|
|
34
|
+
returnUrl = data.returnUrl
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.error('Invalid state:', state)
|
|
37
|
+
returnUrl = '/'
|
|
38
|
+
}
|
|
30
39
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
{ expiresIn: '1d' }
|
|
40
|
-
)
|
|
40
|
+
try {
|
|
41
|
+
const { access_token } = await getAccessToken({
|
|
42
|
+
code,
|
|
43
|
+
GOOGLE_CLIENT_ID,
|
|
44
|
+
GOOGLE_CLIENT_SECRET,
|
|
45
|
+
REDIRECT_URI
|
|
46
|
+
})
|
|
47
|
+
const info = await getUserInfo({ access_token })
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
const { email, name, picture } = info
|
|
50
|
+
const sessionToken = jwt.sign(
|
|
51
|
+
{
|
|
52
|
+
email,
|
|
53
|
+
name,
|
|
54
|
+
picture
|
|
55
|
+
},
|
|
56
|
+
JWT_SECRET,
|
|
57
|
+
{ expiresIn: '1d' }
|
|
58
|
+
)
|
|
43
59
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
60
|
+
return {
|
|
61
|
+
statusCode: 302,
|
|
62
|
+
headers: {
|
|
63
|
+
Location: `${returnUrl}#token=${sessionToken}`
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error('Error:', error)
|
|
68
|
+
return {
|
|
69
|
+
statusCode: 302,
|
|
70
|
+
headers: {
|
|
71
|
+
Location: `${returnUrl}#authError=${error}`
|
|
72
|
+
}
|
|
73
|
+
}
|
|
55
74
|
}
|
|
56
75
|
}
|
|
57
76
|
}
|
|
58
77
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
78
|
+
function verify({ JWT_SECRET }) {
|
|
79
|
+
return async event => {
|
|
80
|
+
try {
|
|
81
|
+
const { token } = JSON.parse(event.body)
|
|
82
|
+
const decoded = jwt.verify(token, JWT_SECRET)
|
|
63
83
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
84
|
+
return {
|
|
85
|
+
statusCode: 200,
|
|
86
|
+
headers: {
|
|
87
|
+
'Access-Control-Allow-Origin': '*',
|
|
88
|
+
'Access-Control-Allow-Credentials': true
|
|
89
|
+
},
|
|
90
|
+
body: JSON.stringify({ user: decoded })
|
|
91
|
+
}
|
|
92
|
+
} catch (error) {
|
|
93
|
+
return {
|
|
94
|
+
statusCode: 401,
|
|
95
|
+
headers: {
|
|
96
|
+
'Access-Control-Allow-Origin': '*',
|
|
97
|
+
'Access-Control-Allow-Credentials': true
|
|
98
|
+
},
|
|
99
|
+
body: JSON.stringify({ error: 'Invalid token' })
|
|
100
|
+
}
|
|
80
101
|
}
|
|
81
102
|
}
|
|
82
103
|
}
|