-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.js
213 lines (195 loc) · 5.87 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const asyncWrapper = require('../async_wrapper');
const logger = require('../logger');
const helperGoogle = require('./google/helper');
const helperJwt = require('./jwt/helper');
const helperEMail = require('./email/helper');
const {
isMongoDB
} = require('../helper');
const errors = require('../errors');
const {
AUTH_TYPE_EMAIL,
AUTH_TYPE_GOOGLE
} = require('../constants');
/**
* Controller : Sing In
* HTTP Method : POST
* PATH : /signin
*
* @param {Object} options
* @param {Sequelize.model} options.admin_users
* @param {String} options.super_role
* @param {Object} options.auth_jwt
* @returns {function(*, *, *)}
*/
const registerSignIn = options => {
const AdminUsers = options.admin_users;
const superRole = options.super_role;
const authJwt = options.auth_jwt;
return asyncWrapper(async (req, res) => {
const email = req.body.email;
const password = req.body.password;
// メアドでユーザ検索
let adminUser;
if (isMongoDB(AdminUsers)) { // MongoDB
adminUser = await AdminUsers.findOne({
email: email
});
} else { // MySQL
adminUser = await AdminUsers.findOne({
where: {
email
}
});
}
if (!adminUser) {
// 1人目かどうか
const cnt = await AdminUsers.count();
if (cnt > 0) {
// 1人目じゃなければエラー(管理者がユーザー作成してあげる)
throw errors.frontend.AdminUserNotFound();
}
// 1人目の場合はスーパーユーザーとして登録する
// - パスワードソルトの生成
const salt = await helperEMail.genSalt();
// - パスワードのハッシュ化
const hashedPassword = await helperEMail.genHash(password, salt);
adminUser = await AdminUsers.create({
salt,
email,
password: hashedPassword,
role_id: superRole,
auth_type: AUTH_TYPE_EMAIL
});
}
// パスワード検証
const verified = await helperEMail.verify(password, adminUser.password, adminUser.salt);
if (!verified) {
return res.json(errors.frontend.SigninFailed());
}
// JWTを生成
const claims = {
sub: email
};
const token = await helperJwt.sign(claims, authJwt);
res.setHeader(authJwt.header_key, `Bearer ${token}`);
return res.end();
});
};
/**
* Controller : Sing Out
* HTTP Method : POST
* PATH : /signout
*
* @returns {function(*, *, *)}
*/
const registerSignOut = () => {
return asyncWrapper(async (req, res) => {
return res.end();
});
};
/**
* Controller : Sing In (Google)
* HTTP Method : POST
* PATH : /googlesignin
*
* @param {Object} options
* @param {Object} options.google_oauth
* @returns {function(*, *, *)}
*/
const registerGoogleSignIn = options => {
const googleOAuth = options.google_oauth;
if (!googleOAuth) {
logger.info('[VIRONLIB] auth /googlesignin skip.');
return asyncWrapper(async (req, res) => {
logger.error('[VIRONLIB] auth /googlesignin is not registered.');
return res.json(errors.frontend.NotFound());
});
}
return asyncWrapper(async (req, res) => {
// Googleの認証画面にリダイレクト
const authUrl = helperGoogle.genAuthUrl(googleOAuth, req.query.redirect_url || req.get('referer'));
return res.redirect(authUrl); // 301
});
};
/**
* Controller : OAuth callback (Google)
* HTTP Method : GET
* PATH : /googleoauth2callback
*
* @param {Object} options
* @param {Sequelize.model} options.admin_users
* @param {Object} options.google_oauth
* @param {Object} options.auth_jwt
* @returns {function(*, *, *)}
*/
const registerGoogleOAuth2Callback = options => {
const AdminUsers = options.admin_users;
const googleOAuth = options.google_oauth;
const authJwt = options.auth_jwt;
const superRole = options.super_role;
const defaultRole = options.default_role;
if (!googleOAuth) {
logger.info('[VIRONLIB] auth /googleoauth2callback skip.');
return asyncWrapper(async (req, res) => {
logger.error('[VIRONLIB] auth /googleoauth2callback is not registered.');
return res.json(errors.frontend.NotFound());
});
}
return asyncWrapper(async (req, res) => {
const redirectUrl = req.query.state;
try {
// アクセストークンを取得
const token = await helperGoogle.getToken(req.query.code, googleOAuth);
// メールアドレスを検証
const email = await helperGoogle.allowMailDomain(token, googleOAuth);
if (!email) {
throw errors.frontend.Forbidden();
}
// メアドでユーザ検索
let adminUser;
if (isMongoDB(AdminUsers)) { // MongoDB
adminUser = await AdminUsers.findOne({
email: email
});
} else { // MySQL
adminUser = await AdminUsers.findOne({
where: {
email
}
});
}
if (!adminUser) {
// 1人目かどうか
const cnt = await AdminUsers.count();
// 1人目の場合はスーパーユーザー、2人目以降はデフォルトロールで登録する
const roleId = cnt > 0 ? defaultRole : superRole;
adminUser = await AdminUsers.create({
email: email,
role_id: roleId,
auth_type: AUTH_TYPE_GOOGLE
});
}
// JWTを生成
const claims = {
sub: email,
googleOAuthToken: token,
};
const jwt = await helperJwt.sign(claims, authJwt);
const authToken = `Bearer ${jwt}`;
res.setHeader(authJwt.header_key, authToken);
return res.redirect(`${redirectUrl}?token=${authToken}`);
} catch (e) {
logger.error(e);
return res.redirect(redirectUrl);
}
});
};
module.exports = options => {
return {
signIn: registerSignIn(options),
signOut: registerSignOut(options),
googleSignIn: registerGoogleSignIn(options),
googleOAuth2Callback: registerGoogleOAuth2Callback(options),
};
};