@things-factory/auth-google 6.1.186
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/CHANGELOG.md +8 -0
- package/assets/images/hatiolab-logo.png +0 -0
- package/client/bootstrap.ts +1 -0
- package/client/index.ts +0 -0
- package/client/route.ts +4 -0
- package/client/tsconfig.json +11 -0
- package/config/config.development.js +19 -0
- package/config/config.production.js +19 -0
- package/dist-client/bootstrap.d.ts +1 -0
- package/dist-client/bootstrap.js +2 -0
- package/dist-client/bootstrap.js.map +1 -0
- package/dist-client/index.d.ts +0 -0
- package/dist-client/index.js +2 -0
- package/dist-client/index.js.map +1 -0
- package/dist-client/route.d.ts +1 -0
- package/dist-client/route.js +5 -0
- package/dist-client/route.js.map +1 -0
- package/dist-client/tsconfig.tsbuildinfo +1 -0
- package/dist-server/controllers/get-access-token.js +30 -0
- package/dist-server/controllers/get-access-token.js.map +1 -0
- package/dist-server/controllers/subscribe-users-change.js +33 -0
- package/dist-server/controllers/subscribe-users-change.js.map +1 -0
- package/dist-server/controllers/sync-user-info.js +95 -0
- package/dist-server/controllers/sync-user-info.js.map +1 -0
- package/dist-server/index.js +19 -0
- package/dist-server/index.js.map +1 -0
- package/dist-server/middlewares/google-authenticate-middleware.js +62 -0
- package/dist-server/middlewares/google-authenticate-middleware.js.map +1 -0
- package/dist-server/middlewares/index.js +13 -0
- package/dist-server/middlewares/index.js.map +1 -0
- package/dist-server/router/auth-google-router.js +11 -0
- package/dist-server/router/auth-google-router.js.map +1 -0
- package/dist-server/router/auth-google-webhook-router.js +73 -0
- package/dist-server/router/auth-google-webhook-router.js.map +1 -0
- package/dist-server/router/index.js +6 -0
- package/dist-server/router/index.js.map +1 -0
- package/dist-server/routes.js +23 -0
- package/dist-server/routes.js.map +1 -0
- package/dist-server/service/auth-google/auth-google-mutation.js +32 -0
- package/dist-server/service/auth-google/auth-google-mutation.js.map +1 -0
- package/dist-server/service/auth-google/index.js +6 -0
- package/dist-server/service/auth-google/index.js.map +1 -0
- package/dist-server/service/index.js +19 -0
- package/dist-server/service/index.js.map +1 -0
- package/dist-server/tsconfig.tsbuildinfo +1 -0
- package/helps/auth-google/auth-google.md +160 -0
- package/package.json +38 -0
- package/server/controllers/get-access-token.ts +30 -0
- package/server/controllers/subscribe-users-change.ts +34 -0
- package/server/controllers/sync-user-info.ts +129 -0
- package/server/index.ts +18 -0
- package/server/middlewares/google-authenticate-middleware.ts +70 -0
- package/server/middlewares/index.ts +13 -0
- package/server/router/auth-google-router.ts +14 -0
- package/server/router/auth-google-webhook-router.ts +96 -0
- package/server/router/index.ts +2 -0
- package/server/routes.ts +28 -0
- package/server/service/auth-google/auth-google-mutation.ts +24 -0
- package/server/service/auth-google/index.ts +3 -0
- package/server/service/index.ts +19 -0
- package/server/tsconfig.json +10 -0
- package/things-factory.config.js +8 -0
- package/translations/en.json +1 -0
- package/translations/ja.json +1 -0
- package/translations/ko.json +1 -0
- package/translations/ms.json +1 -0
- package/translations/zh.json +1 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# AuthGoogle
|
|
2
|
+
|
|
3
|
+
Paragraphs are separated by a blank line.
|
|
4
|
+
|
|
5
|
+
2nd paragraph. _Italic_, **bold**, and `monospace`. Itemized lists
|
|
6
|
+
look like:
|
|
7
|
+
|
|
8
|
+
- this one
|
|
9
|
+
- that one
|
|
10
|
+
- the other one
|
|
11
|
+
|
|
12
|
+
Note that --- not considering the asterisk --- the actual text
|
|
13
|
+
content starts at 4-columns in.
|
|
14
|
+
|
|
15
|
+
> Block quotes are
|
|
16
|
+
> written like so.
|
|
17
|
+
>
|
|
18
|
+
> They can span multiple paragraphs,
|
|
19
|
+
> if you like.
|
|
20
|
+
|
|
21
|
+
Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
|
|
22
|
+
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
|
|
23
|
+
Unicode is supported. ☺
|
|
24
|
+
|
|
25
|
+
## An h2 header
|
|
26
|
+
|
|
27
|
+
Here's a numbered list:
|
|
28
|
+
|
|
29
|
+
1. first item
|
|
30
|
+
2. second item
|
|
31
|
+
3. third item
|
|
32
|
+
|
|
33
|
+
Note again how the actual text starts at 4 columns in (4 characters
|
|
34
|
+
from the left side). Here's a code sample:
|
|
35
|
+
|
|
36
|
+
# Let me re-iterate ...
|
|
37
|
+
|
|
38
|
+
for i in 1 .. 10 { do-something(i) }
|
|
39
|
+
|
|
40
|
+
As you probably guessed, indented 4 spaces. By the way, instead of
|
|
41
|
+
indenting the block, you can use delimited blocks, if you like:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
define foobar() {
|
|
45
|
+
print "Welcome to flavor country!";
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
(which makes copying & pasting easier). You can optionally mark the
|
|
50
|
+
delimited block for Pandoc to syntax highlight it:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
import time
|
|
54
|
+
# Quick, count to ten!
|
|
55
|
+
for i in range(10):
|
|
56
|
+
# (but not *too* quick)
|
|
57
|
+
time.sleep(0.5)
|
|
58
|
+
print(i)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### An h3 header
|
|
62
|
+
|
|
63
|
+
Now a nested list:
|
|
64
|
+
|
|
65
|
+
1. First, get these ingredients:
|
|
66
|
+
|
|
67
|
+
- carrots
|
|
68
|
+
- celery
|
|
69
|
+
- lentils
|
|
70
|
+
|
|
71
|
+
2. Boil some water.
|
|
72
|
+
|
|
73
|
+
3. Dump everything in the pot and follow
|
|
74
|
+
this algorithm:
|
|
75
|
+
|
|
76
|
+
find wooden spoon
|
|
77
|
+
uncover pot
|
|
78
|
+
stir
|
|
79
|
+
cover pot
|
|
80
|
+
balance wooden spoon precariously on pot handle
|
|
81
|
+
wait 10 minutes
|
|
82
|
+
goto first step (or shut off burner when done)
|
|
83
|
+
|
|
84
|
+
Do not bump wooden spoon or it will fall.
|
|
85
|
+
|
|
86
|
+
Notice again how text always lines up on 4-space indents (including
|
|
87
|
+
that last line which continues item 3 above).
|
|
88
|
+
|
|
89
|
+
Here's a link to [a website](http://foo.bar), to a [local
|
|
90
|
+
doc](local-doc.html), and to a [section heading in the current
|
|
91
|
+
doc](#an-h2-header). Here's a footnote [^1].
|
|
92
|
+
|
|
93
|
+
[^1]: Some footnote text.
|
|
94
|
+
|
|
95
|
+
Tables can look like this:
|
|
96
|
+
|
|
97
|
+
Name Size Material Color
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
All Business 9 leather brown
|
|
102
|
+
Roundabout 10 hemp canvas natural
|
|
103
|
+
Cinderella 11 glass transparent
|
|
104
|
+
|
|
105
|
+
Table: Shoes sizes, materials, and colors.
|
|
106
|
+
|
|
107
|
+
(The above is the caption for the table.) Pandoc also supports
|
|
108
|
+
multi-line tables:
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
Keyword Text
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
red Sunsets, apples, and
|
|
117
|
+
other red or reddish
|
|
118
|
+
things.
|
|
119
|
+
|
|
120
|
+
green Leaves, grass, frogs
|
|
121
|
+
and other things it's
|
|
122
|
+
not easy being.
|
|
123
|
+
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
A horizontal rule follows.
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
|
|
130
|
+
Here's a definition list:
|
|
131
|
+
|
|
132
|
+
apples
|
|
133
|
+
: Good for making applesauce.
|
|
134
|
+
|
|
135
|
+
oranges
|
|
136
|
+
: Citrus!
|
|
137
|
+
|
|
138
|
+
tomatoes
|
|
139
|
+
: There's no "e" in tomatoe.
|
|
140
|
+
|
|
141
|
+
Again, text is indented 4 spaces. (Put a blank line between each
|
|
142
|
+
term and its definition to spread things out more.)
|
|
143
|
+
|
|
144
|
+
Here's a "line block" (note how whitespace is honored):
|
|
145
|
+
|
|
146
|
+
| Line one
|
|
147
|
+
| Line too
|
|
148
|
+
| Line tree
|
|
149
|
+
|
|
150
|
+
and images can be specified like so:
|
|
151
|
+
|
|
152
|
+

|
|
153
|
+
|
|
154
|
+
Inline math equation: $\omega = d\phi / dt$. Display
|
|
155
|
+
math should get its own line like so:
|
|
156
|
+
|
|
157
|
+
$$I = \int \rho R^{2} dV$$
|
|
158
|
+
|
|
159
|
+
And note that you can backslash-escape any punctuation characters
|
|
160
|
+
which you wish to be displayed literally, ex.: \`foo\`, \*bar\*, etc.
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@things-factory/auth-google",
|
|
3
|
+
"version": "6.1.186",
|
|
4
|
+
"main": "dist-server/index.js",
|
|
5
|
+
"browser": "dist-client/index.js",
|
|
6
|
+
"things-factory": true,
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "heartyoh <heartyoh@hatiolab.com>",
|
|
9
|
+
"description": "Module for Supporting User Authentication with Google",
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public",
|
|
12
|
+
"@things-factory:registry": "https://registry.npmjs.org"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/hatiolab/things-factory.git",
|
|
17
|
+
"directory": "packages/auth-google"
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "npm run build:server && npm run build:client",
|
|
21
|
+
"copy:files": "copyfiles -e \"./client/**/*.{ts,js,json}\" -u 1 \"./client/**/*\" dist-client",
|
|
22
|
+
"build:client": "npm run copy:files && npx tsc --p ./client/tsconfig.json",
|
|
23
|
+
"build:server": "npx tsc --p ./server/tsconfig.json",
|
|
24
|
+
"clean:client": "npx rimraf dist-client",
|
|
25
|
+
"clean:server": "npx rimraf dist-server",
|
|
26
|
+
"clean": "npm run clean:server && npm run clean:client",
|
|
27
|
+
"migration:create": "node ../../node_modules/typeorm/cli.js migration:create -d ./server/migrations"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@operato/graphql": "^1.0.0",
|
|
31
|
+
"@operato/shell": "^1.0.0",
|
|
32
|
+
"@things-factory/auth-base": "^6.1.186",
|
|
33
|
+
"@things-factory/shell": "^6.1.186",
|
|
34
|
+
"passport": "^0.6.0",
|
|
35
|
+
"passport-google-oauth20": "^2.0.0"
|
|
36
|
+
},
|
|
37
|
+
"gitHead": "ddf508496cdd7d4b9d0e020703a2f7a586a42ec5"
|
|
38
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
|
|
3
|
+
import { AuthProvider } from '@things-factory/auth-base'
|
|
4
|
+
|
|
5
|
+
const authorityHostUrl = 'https://login.microsoftonline.com'
|
|
6
|
+
|
|
7
|
+
export async function getAccessToken(authProvider: AuthProvider): Promise<string | null> {
|
|
8
|
+
try {
|
|
9
|
+
const { clientId, clientSecret, tenantId } = authProvider
|
|
10
|
+
|
|
11
|
+
const authorityUrl = `${authorityHostUrl}/${tenantId}`
|
|
12
|
+
const resource = 'https://graph.microsoft.com'
|
|
13
|
+
const OAUTH2_URL = `${authorityUrl}/oauth2/token?api-version=1.0`
|
|
14
|
+
|
|
15
|
+
const tokenResponse = await fetch(OAUTH2_URL, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers: {
|
|
18
|
+
'Content-Type': 'application/x-www-form-urlencoded'
|
|
19
|
+
},
|
|
20
|
+
body: `client_id=${clientId}&resource=${resource}&client_secret=${clientSecret}&grant_type=client_credentials`
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const result = await tokenResponse.json()
|
|
24
|
+
const { access_token } = result
|
|
25
|
+
return access_token
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.error('Error fetching access token:', error)
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
|
|
3
|
+
import { getAccessToken } from './get-access-token'
|
|
4
|
+
import { AuthProvider } from '@things-factory/auth-base'
|
|
5
|
+
|
|
6
|
+
var clientState
|
|
7
|
+
|
|
8
|
+
export function getClientState() {
|
|
9
|
+
return clientState
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function msgraphSubscriptions(authProvider: AuthProvider) {
|
|
13
|
+
const accessToken = await getAccessToken(authProvider)
|
|
14
|
+
const expirationDateTime = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000)
|
|
15
|
+
|
|
16
|
+
clientState = `${authProvider.domainId}:${authProvider.id}` // TODO make secure
|
|
17
|
+
|
|
18
|
+
const response = await fetch('https://graph.microsoft.com/v1.0/subscriptions', {
|
|
19
|
+
method: 'POST',
|
|
20
|
+
headers: {
|
|
21
|
+
Authorization: `Bearer ${accessToken}`,
|
|
22
|
+
'Content-Type': 'application/json'
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({
|
|
25
|
+
changeType: 'created,updated',
|
|
26
|
+
notificationUrl: 'YOUR_WEBHOOK_ENDPOINT_BASE' + '/webhook/google-users',
|
|
27
|
+
resource: '/users',
|
|
28
|
+
expirationDateTime: expirationDateTime.toISOString() /* 최대 3일 이내, UTC 타임으로 설정 */,
|
|
29
|
+
clientState
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const data = await response.json()
|
|
34
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
|
|
3
|
+
import { AuthProvider, User, UserStatus, UsersAuthProviders } from '@things-factory/auth-base'
|
|
4
|
+
import { getAccessToken } from './get-access-token'
|
|
5
|
+
|
|
6
|
+
export async function syncAllUserInfo(authProvider: AuthProvider, context: ResolverContext) {
|
|
7
|
+
const accessToken = await getAccessToken(authProvider)
|
|
8
|
+
|
|
9
|
+
if (!accessToken) {
|
|
10
|
+
throw new Error('Failed to obtain access token')
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const latestData = await fetch('https://graph.microsoft.com/v1.0/users', {
|
|
14
|
+
headers: {
|
|
15
|
+
Authorization: `Bearer ${accessToken}`
|
|
16
|
+
}
|
|
17
|
+
}).then(res => res.json())
|
|
18
|
+
|
|
19
|
+
latestData.value
|
|
20
|
+
.filter(({ mail }) => mail)
|
|
21
|
+
.forEach(async user => {
|
|
22
|
+
await updateUserInfo(authProvider, user, context)
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function updateUserInfo(authProvider: AuthProvider, userInfo, context: ResolverContext) {
|
|
27
|
+
const { tx, domain, user } = context.state
|
|
28
|
+
|
|
29
|
+
// {
|
|
30
|
+
// "id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
|
|
31
|
+
// "businessPhones": [
|
|
32
|
+
// "+1 412 555 0109"
|
|
33
|
+
// ],
|
|
34
|
+
// "displayName": "John Doe",
|
|
35
|
+
// "givenName": "John",
|
|
36
|
+
// "jobTitle": "Developer",
|
|
37
|
+
// "mail": "john.doe@contoso.com",
|
|
38
|
+
// "mobilePhone": "+1 412 555 0109",
|
|
39
|
+
// "officeLocation": "Floor 2",
|
|
40
|
+
// "preferredLanguage": "en-US",
|
|
41
|
+
// "surname": "Doe",
|
|
42
|
+
// "userPrincipalName": "john.doe@contoso.com"
|
|
43
|
+
// }
|
|
44
|
+
const {
|
|
45
|
+
id,
|
|
46
|
+
businessPhones,
|
|
47
|
+
displayName,
|
|
48
|
+
givenName,
|
|
49
|
+
surname,
|
|
50
|
+
jobTitle,
|
|
51
|
+
mail,
|
|
52
|
+
mobilePhone,
|
|
53
|
+
officeLocation,
|
|
54
|
+
preferredLanguage,
|
|
55
|
+
userPricipalName
|
|
56
|
+
} = userInfo
|
|
57
|
+
|
|
58
|
+
const repository = tx.getRepository(User)
|
|
59
|
+
|
|
60
|
+
// 1. 사용자를 찾는다.(email 정보로)
|
|
61
|
+
const existingUser = await repository.findOne({
|
|
62
|
+
where: { email: mail },
|
|
63
|
+
relations: ['domains']
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
if (!existingUser) {
|
|
67
|
+
// 2. 사용자가 없다면, 생성한다.
|
|
68
|
+
const salt = User.generateSalt()
|
|
69
|
+
|
|
70
|
+
/* normally they don't login with this password. */
|
|
71
|
+
const password = salt
|
|
72
|
+
|
|
73
|
+
const createdUser = await repository.save({
|
|
74
|
+
id,
|
|
75
|
+
name: displayName,
|
|
76
|
+
email: mail,
|
|
77
|
+
creator: user,
|
|
78
|
+
updater: user,
|
|
79
|
+
domains: domain ? [domain] : [],
|
|
80
|
+
roles: [],
|
|
81
|
+
salt,
|
|
82
|
+
userType: 'user',
|
|
83
|
+
// ssoId: id,
|
|
84
|
+
locale: preferredLanguage,
|
|
85
|
+
status: UserStatus.ACTIVATED,
|
|
86
|
+
passwordUpdatedAt: new Date(),
|
|
87
|
+
password: User.encode(password, salt)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
await tx.getRepository(UsersAuthProviders).save({
|
|
91
|
+
domain,
|
|
92
|
+
user: createdUser,
|
|
93
|
+
authProvider,
|
|
94
|
+
ssoId: id
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
return createdUser
|
|
98
|
+
} else {
|
|
99
|
+
// 3. 사용자가 있다면, 업데이트한다.
|
|
100
|
+
const { domains } = existingUser
|
|
101
|
+
|
|
102
|
+
if (!domains.find(existing => existing.id == domain.id)) {
|
|
103
|
+
domains.push(domain)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const updatedUser = await repository.save({
|
|
107
|
+
...existingUser,
|
|
108
|
+
name: displayName,
|
|
109
|
+
// ssoId: id,
|
|
110
|
+
domains,
|
|
111
|
+
locale: preferredLanguage,
|
|
112
|
+
updater: user
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
const usersAuthProviders = await tx.getRepository(UsersAuthProviders).findOne({
|
|
116
|
+
where: { domain: { id: domain.id }, user: { id: updatedUser.id }, authProvider: { id: authProvider.id } }
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
await tx.getRepository(UsersAuthProviders).save({
|
|
120
|
+
...usersAuthProviders,
|
|
121
|
+
domain,
|
|
122
|
+
user: updatedUser,
|
|
123
|
+
authProvider,
|
|
124
|
+
ssoId: id
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
return updatedUser
|
|
128
|
+
}
|
|
129
|
+
}
|
package/server/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export * from './middlewares'
|
|
2
|
+
export * from './service'
|
|
3
|
+
|
|
4
|
+
import './routes'
|
|
5
|
+
|
|
6
|
+
import { AuthProvider } from '@things-factory/auth-base'
|
|
7
|
+
import { syncAllUserInfo } from './controllers/sync-user-info'
|
|
8
|
+
|
|
9
|
+
AuthProvider.register('google', {
|
|
10
|
+
type: 'google',
|
|
11
|
+
description: 'Authentication Provider for Google',
|
|
12
|
+
help: '',
|
|
13
|
+
parameterSpec: null,
|
|
14
|
+
async synchronizeUsers(authProvider: AuthProvider, context: ResolverContext): Promise<boolean> {
|
|
15
|
+
await syncAllUserInfo(authProvider, context)
|
|
16
|
+
return true
|
|
17
|
+
}
|
|
18
|
+
})
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import passport from 'koa-passport'
|
|
2
|
+
import { Strategy as GoogleStrategy } from 'passport-google-oauth20'
|
|
3
|
+
|
|
4
|
+
import { config } from '@things-factory/env'
|
|
5
|
+
import { User } from '@things-factory/auth-base'
|
|
6
|
+
|
|
7
|
+
const SSOGoogleConfig = config.get('sso/google/config')
|
|
8
|
+
|
|
9
|
+
if (SSOGoogleConfig) {
|
|
10
|
+
passport.use(
|
|
11
|
+
new GoogleStrategy(SSOGoogleConfig, async (token, tokenSecret, profile, done) => {
|
|
12
|
+
if (!profile) {
|
|
13
|
+
return done(new Error('No profile found'), null)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const { email } = profile
|
|
18
|
+
|
|
19
|
+
const user = await User.checkAuthWithEmail({ email })
|
|
20
|
+
|
|
21
|
+
const { id, userType, status } = user
|
|
22
|
+
|
|
23
|
+
return done(null, {
|
|
24
|
+
id,
|
|
25
|
+
userType,
|
|
26
|
+
status
|
|
27
|
+
})
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return done(error)
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function authGoogleMiddleware(context, next) {
|
|
36
|
+
const { path } = context
|
|
37
|
+
const { user } = context.state
|
|
38
|
+
|
|
39
|
+
if (user) {
|
|
40
|
+
return await next()
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await passport.authenticate('google', {
|
|
44
|
+
failureRedirect: '/auth/signin',
|
|
45
|
+
successRedirect: '/auth/checkin'
|
|
46
|
+
})(context, next)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function googleSubscriptionMiddleware(context, next) {
|
|
50
|
+
const { path } = context
|
|
51
|
+
const { user } = context.state
|
|
52
|
+
|
|
53
|
+
if (user) {
|
|
54
|
+
return await next()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
await passport.authenticate('google', {
|
|
58
|
+
failureRedirect: '/auth/signin',
|
|
59
|
+
successRedirect: '/auth/checkin'
|
|
60
|
+
})(context)
|
|
61
|
+
|
|
62
|
+
/* 다음은, websocket 에서 인증을 처리하기 위한 작업임. */
|
|
63
|
+
const passportObject = context.session?.passport
|
|
64
|
+
if (passportObject) {
|
|
65
|
+
const userEntity = await User.checkAuth(passportObject?.user)
|
|
66
|
+
context.state.user = userEntity
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return await next()
|
|
70
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { config } from '@things-factory/env'
|
|
2
|
+
|
|
3
|
+
const SSOGoogleConfig = config.get('sso/google/config')
|
|
4
|
+
|
|
5
|
+
import { googleSubscriptionMiddleware } from './google-authenticate-middleware'
|
|
6
|
+
|
|
7
|
+
if (SSOGoogleConfig) {
|
|
8
|
+
process.on('bootstrap-module-subscription' as any, (app, subscriptionMiddleware) => {
|
|
9
|
+
subscriptionMiddleware.unshift(googleSubscriptionMiddleware)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export * from './google-authenticate-middleware'
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Router from 'koa-router'
|
|
2
|
+
|
|
3
|
+
import passport from 'koa-passport'
|
|
4
|
+
import { authGoogleMiddleware } from '../middlewares'
|
|
5
|
+
|
|
6
|
+
export const authGoogleRouter = new Router()
|
|
7
|
+
|
|
8
|
+
authGoogleRouter.get(
|
|
9
|
+
'/auth/callback-auth-google',
|
|
10
|
+
passport.authenticate('google', { failureRedirect: '/auth/signin' }),
|
|
11
|
+
ctx => {
|
|
12
|
+
ctx.redirect('/auth/checkin')
|
|
13
|
+
}
|
|
14
|
+
)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import Router from 'koa-router'
|
|
2
|
+
import fetch from 'node-fetch'
|
|
3
|
+
|
|
4
|
+
import { getRepository, getDataSource } from '@things-factory/shell'
|
|
5
|
+
|
|
6
|
+
import { updateUserInfo } from '../controllers/sync-user-info'
|
|
7
|
+
import { getAccessToken } from '../controllers/get-access-token'
|
|
8
|
+
import { AuthProvider } from '@things-factory/auth-base'
|
|
9
|
+
|
|
10
|
+
export const authGoogleWebhookRouter = new Router()
|
|
11
|
+
|
|
12
|
+
// Microsoft Graph에서 발송되는 알림을 처리하기 위한 타입 정의
|
|
13
|
+
interface Notification {
|
|
14
|
+
resourceData: {
|
|
15
|
+
id: string
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
authGoogleWebhookRouter.post('/webhook/google-users', async context => {
|
|
20
|
+
const notifications: Notification[] = context.request.body.value
|
|
21
|
+
const clientState = context.request.body.clientState
|
|
22
|
+
const [domainId, authProviderId] = clientState?.split(':') || []
|
|
23
|
+
|
|
24
|
+
if (!domainId || !authProviderId) {
|
|
25
|
+
context.status = 401 // unauthorized
|
|
26
|
+
context.body = 'Invalid ClientState'
|
|
27
|
+
return
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
await getDataSource('tx').transaction(async tx => {
|
|
31
|
+
const wrap = context.app.createContext(context.req || {}, context.res || {})
|
|
32
|
+
|
|
33
|
+
wrap.state = {
|
|
34
|
+
...context.state,
|
|
35
|
+
domain: null,
|
|
36
|
+
tx
|
|
37
|
+
}
|
|
38
|
+
wrap.t = context.t
|
|
39
|
+
|
|
40
|
+
const authProvider = await tx.getRepository(AuthProvider).findOne({
|
|
41
|
+
where: {
|
|
42
|
+
domain: { id: domainId },
|
|
43
|
+
id: authProviderId
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
if (!authProvider) {
|
|
48
|
+
context.status = 401 // unauthorized
|
|
49
|
+
context.body = 'Invalid ClientState'
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const accessToken = await getAccessToken(authProvider)
|
|
54
|
+
|
|
55
|
+
for (const notification of notifications) {
|
|
56
|
+
const userId = notification.resourceData.id
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const userResponse = await fetch(`https://graph.microsoft.com/v1.0/users/${userId}`, {
|
|
60
|
+
headers: {
|
|
61
|
+
Authorization: `Bearer ${accessToken}`
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
if (!userResponse.ok) {
|
|
66
|
+
throw new Error(`Failed to fetch user data: ${userResponse.statusText}`)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const userInfo = await userResponse.json()
|
|
70
|
+
|
|
71
|
+
// {
|
|
72
|
+
// "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity",
|
|
73
|
+
// "businessPhones": ["+1 412 555 0109"],
|
|
74
|
+
// "displayName": "Megan Bowen",
|
|
75
|
+
// "givenName": "Megan",
|
|
76
|
+
// "jobTitle": "Auditor",
|
|
77
|
+
// "mail": "MeganB@M365x214355.onmicrosoft.com",
|
|
78
|
+
// "mobilePhone": "+1 412 555 0109",
|
|
79
|
+
// "officeLocation": "12/1110",
|
|
80
|
+
// "preferredLanguage": "en-US",
|
|
81
|
+
// "surname": "Bowen",
|
|
82
|
+
// "userPrincipalName": "MeganB@M365x214355.onmicrosoft.com",
|
|
83
|
+
// "id": "48d31887-5fad-4d73-a9f5-3c356e68a038"
|
|
84
|
+
// }
|
|
85
|
+
|
|
86
|
+
// webhook 호출로부터 호출한 tenant를 알 수 있다면, 매핑된 도메인 정보를 포함할 수 있을 것 같은데..
|
|
87
|
+
|
|
88
|
+
await updateUserInfo(authProvider, userInfo, wrap)
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error('Error fetching user data from Microsoft Graph:', error)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
context.status = 202
|
|
96
|
+
})
|
package/server/routes.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import passport from 'koa-passport'
|
|
2
|
+
import { config } from '@things-factory/env'
|
|
3
|
+
import { setAccessTokenCookie } from '@things-factory/auth-base'
|
|
4
|
+
|
|
5
|
+
const SSOGoogleConfig = config.get('sso/google/config')
|
|
6
|
+
|
|
7
|
+
import { authGoogleMiddleware } from './middlewares'
|
|
8
|
+
import { authGoogleRouter, authGoogleWebhookRouter } from './router'
|
|
9
|
+
|
|
10
|
+
if (SSOGoogleConfig) {
|
|
11
|
+
process.on('bootstrap-collect-sso-middleware' as any, (_, ssoMiddlewares) => {
|
|
12
|
+
ssoMiddlewares.push(authGoogleMiddleware)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
process.on('bootstrap-module-domain-public-route' as any, (app, domainPublicRouter) => {
|
|
16
|
+
domainPublicRouter.use(authGoogleRouter.routes(), authGoogleRouter.allowedMethods())
|
|
17
|
+
domainPublicRouter.use(authGoogleWebhookRouter.routes(), authGoogleWebhookRouter.allowedMethods())
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
process.on('bootstrap-module-global-public-route' as any, (app, globalPublicRouter) => {
|
|
21
|
+
globalPublicRouter.get(
|
|
22
|
+
'/auth/signin-with-google',
|
|
23
|
+
passport.authenticate('google', {
|
|
24
|
+
scope: ['https://www.googleapis.com/auth/plus.login']
|
|
25
|
+
})
|
|
26
|
+
)
|
|
27
|
+
})
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Resolver, Mutation, Arg, Ctx, Directive } from 'type-graphql'
|
|
2
|
+
|
|
3
|
+
import { syncAllUserInfo } from '../../controllers/sync-user-info'
|
|
4
|
+
|
|
5
|
+
@Resolver()
|
|
6
|
+
export class AuthGoogleMutation {
|
|
7
|
+
// @Directive('@transaction')
|
|
8
|
+
// @Directive('@privilege(superUserGranted:true)')
|
|
9
|
+
// @Mutation(returns => Boolean, { description: 'To synchronize google users' })
|
|
10
|
+
// async synchronizeGoogleUsers(@Ctx() context: ResolverContext): Promise<boolean> {
|
|
11
|
+
// syncAllUserInfo(context)
|
|
12
|
+
|
|
13
|
+
// return true
|
|
14
|
+
// }
|
|
15
|
+
|
|
16
|
+
@Directive('@transaction')
|
|
17
|
+
@Directive('@privilege(superUserGranted:true)')
|
|
18
|
+
@Mutation(returns => Boolean, { description: 'To subscribe google users' })
|
|
19
|
+
async subscribeGoogleUsers(@Ctx() context: ResolverContext): Promise<boolean> {
|
|
20
|
+
const { domain, user, tx } = context.state
|
|
21
|
+
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/* EXPORT ENTITY TYPES */
|
|
2
|
+
|
|
3
|
+
/* IMPORT ENTITIES AND RESOLVERS */
|
|
4
|
+
import { resolvers as AuthGoogleResolvers } from './auth-google'
|
|
5
|
+
|
|
6
|
+
export const entities = [
|
|
7
|
+
/* ENTITIES */
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
export const subscribers = [
|
|
11
|
+
/* SUBSCRIBERS */
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
export const schema = {
|
|
15
|
+
resolverClasses: [
|
|
16
|
+
/* RESOLVER CLASSES */
|
|
17
|
+
...AuthGoogleResolvers
|
|
18
|
+
]
|
|
19
|
+
}
|