-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
227 lines (202 loc) · 7.72 KB
/
app.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'use strict';
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// functions.logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
// default root url to test if the server is up
// import necessary modules
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request-promise');
// Firebase setup
const firebaseAdmin = require('firebase-admin');
// you should manually put your service-account.json in the same folder app.js
// is located at.
const serviceAccount = require('./service-account.json');
// Kakao API request url to retrieve user profile based on access token
const requestMeUrl = 'https://kapi.kakao.com/v2/user/me?secure_resource=true';
const accessTokenInfoUrl = 'https://kapi.kakao.com/v1/user/access_token_info';
const config = require('./config.json'); // put your kakao app id in config.json
// Initialize FirebaseApp with service-account.json
firebaseAdmin.initializeApp({
credential: firebaseAdmin.credential.cert(serviceAccount)
});
/**
* requestMe - Returns user profile from Kakao API
*
* @param {String} kakaoAccessToken Access token retrieved by Kakao Login API
* @return {Promise<Response>} User profile response in a promise
*/
function requestMe(kakaoAccessToken) {
console.log('Requesting user profile from Kakao API server.');
return request({
method: 'GET',
headers: {'Authorization': 'Bearer ' + kakaoAccessToken},
url: requestMeUrl,
});
}
/**
* validateToken - Returns access token info from Kakao API,
* which checks if this token is issued by this application.
*
* @param {String} kakaoAccessToken Access token retrieved by Kakao Login API
* @return {Promise<Response>} Access token info response
*/
function validateToken(kakaoAccessToken) {
console.log('Validating access token from Kakao API server.');
return request({
method: 'GET',
headers: {'Authorization': 'Bearer ' + kakaoAccessToken},
url: accessTokenInfoUrl,
});
}
/**
* createOrLinkUser - Link firebase user with given email,
* or create one if none exists. If email is not given,
* create a new user since there is no other way to map users.
* If email is not verified, make the user re-authenticate with other means.
*
* @param {String} kakaoUserId user id per app
* @param {String} email user's email address
* @param {Boolean} emailVerified whether this email is verified or not
* @param {String} displayName user
* @param {String} photoURL profile photo url
* @return {Promise<UserRecord>} Firebase user record in a promise
*/
function createOrLinkUser(kakaoUserId, email, emailVerified, displayName,
photoURL) {
return getUser(kakaoUserId, email, emailVerified)
.catch((error) => {
if (error.code === 'auth/user-not-found') {
const params = {
uid: `kakao:${kakaoUserId}`,
displayName: displayName,
};
if (email) {
params['email'] = email;
}
if (photoURL) {
params['photoURL'] = photoURL;
}
console.log(`creating a firebase user with email ${email}`);
return firebaseAdmin.auth().createUser(params);
}
throw error;
})
.then((userRecord) => linkUserWithKakao(kakaoUserId, userRecord));
}
/**
* getUser - fetch firebase user with kakao UID first, then with email if
* no user found. If email is not verified, throw an error so that
* the user can re-authenticate.
*
* @param {String} kakaoUserId user id per app
* @param {String} email user's email address
* @param {Boolean} emailVerified whether this email is verified or not
* @return {Promise<admin.auth.UserRecord>}
*/
function getUser(kakaoUserId, email, emailVerified) {
console.log(`fetching a firebase user with uid kakao:${kakaoUserId}`);
return firebaseAdmin.auth().getUser(`kakao:${kakaoUserId}`)
.catch((error) => {
if (error.code !== 'auth/user-not-found') {
throw error;
}
if (!email) {
throw error; // cannot find existing accounts since there is no email.
}
console.log(`fetching a firebase user with email ${email}`);
return firebaseAdmin.auth().getUserByEmail(email)
.then((userRecord) => {
if (!emailVerified) {
throw new Error('This user should authenticate first ' +
'with other providers');
}
return userRecord;
});
});
}
/**
* linkUserWithKakao - Link current user record with kakao UID
* if not linked yet.
*
* @param {String} kakaoUserId
* @param {admin.auth.UserRecord} userRecord
* @return {Promise<UserRecord>}
*/
function linkUserWithKakao(kakaoUserId, userRecord) {
if (userRecord.customClaims &&
userRecord.customClaims['kakaoUID'] === kakaoUserId) {
console.log(`currently linked with kakao UID ${kakaoUserId}...`);
return Promise.resolve(userRecord);
}
console.log(`linking user with kakao UID ${kakaoUserId}...`);
return firebaseAdmin.auth()
.setCustomUserClaims(userRecord.uid,
{kakaoUID: kakaoUserId}).then(() => userRecord);
}
/**
* createFirebaseToken - returns Firebase token using Firebase Admin SDK
*
* @param {String} kakaoAccessToken access token from Kakao Login API
* @return {Promise<String>} Firebase token in a promise
*/
function createFirebaseToken(kakaoAccessToken) {
return validateToken(kakaoAccessToken).then((response) => {
const body = JSON.parse(response);
const appId = body.appId;
if (appId !== config.kakao.appId) {
throw new Error('The given token does not belong to this application.');
}
return requestMe(kakaoAccessToken);
}).then((response) => {
const body = JSON.parse(response);
console.log(body);
console.log('email:');
console.log(body.kakao_account.email);
console.log(body.kakao_account.is_email_verified);
const userId = body.id;
if (!userId) {
throw new Error('There was no user with the given access token.');
}
let nickname = null;
let profileImage = null;
if (body.properties) {
nickname = body.properties.nickname;
profileImage = body.properties.profile_image;
}
return createOrLinkUser(userId, body.kakao_account.email,
body.kakao_account.is_email_verified, nickname, profileImage);
}).then((userRecord) => {
const userId = userRecord.uid;
console.log(`creating a custom firebase token based on uid ${userId}`);
return firebaseAdmin.auth().createCustomToken(userId, {provider: 'KAKAO'});
});
}
// create an express app and use json body parser
const app = express();
app.use(bodyParser.json());
// default root url to test if the server is up
app.get('/', (req, res) => res.status(200)
.send('KakaoLoginServer for Firebase is up and running!'));
// actual endpoint that creates a firebase token with Kakao access token
app.post('/', (req, res) => {
const token = req.body.token;
if (!token) return res.status(400).send({error: 'There is no token.'})
.send({message: 'Access token is a required parameter.'});
console.log(`Verifying Kakao token: ${token}`);
createFirebaseToken(token).then((firebaseToken) => {
console.log(`Returning firebase token to user: ${firebaseToken}`);
res.send({firebase_token: firebaseToken});
}).catch((error) => res.status(401).send({message: error}));
});
// Start the server
const server = app.listen('8000', () => {
console.log('KakaoLoginServer for Firebase listening on port %s',
server.address().port);
});
exports.verifyToken = functions.https.onRequest(app);