-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmail-hook.bsh
252 lines (195 loc) · 9.68 KB
/
gmail-hook.bsh
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#! /bin/bash
## gmail-hook.bsh -- 2019-02-13
## Bash-shell script to create the Google-feathers login example with a hook entirely from scratch in
## order to extract out the email address, google user information, etc.
## Script is written entirely from https://docs.feathersjs.com/guides/auth/recipe.oauth-basic.html
## Note: expect-tcl is used to run the feathers-cli steps.
## Also, there is a bit of a debate if sudo is required for the feathers installation. I
## am using sudo to make the command global in nature.
## Be sure to run the feathers install gist first!
## a special shout-out THANK you to feathers slackware Freeline for providing me the code to retrieve the login information.
## ./config/default.json changes -- YOU WILL NEED TO CHANGE THESE!
HOST='fastfeathers.website' ;
GOOGLE_KEY='119068144580-gpeiuXXXXXXXXXXXXXXXXXXXXXXXXk5asgs.apps.googleusercontent.com' ;
GOOGLE_SECRET='m-RsXXXXXXXXXXJIajwpN' ;
echo -n 'node version: ' ; node --version ; ## 8.15 at the time of this writing
if [ $? -ne 0 ]; then
echo 'Node has not yet been installed!';
exit 1;
fi
echo -n 'npm version: ' ; npm --version ; ## 6.7.0 at the time of this writing
if [ $? -ne 0 ]; then
echo 'NPM has not yet been installed!';
exit 1;
fi
echo -n 'feathers version: ' ; feathers --version ; ## 3.9 at the time of this writing
if [ $? -ne 0 ]; then
echo 'Feathers has not yet been installed!';
exit 1;
fi
## prompt for any/all constant-variable changes
echo 'get your authentication information from https://console.developers.google.com/apis/credentials/oauthclient/';
read -p "Enter HOST or blank to use '$HOST': " tmpHost;
if [ ! -z "$tmpHost" ]; then
HOST=$tmpHost;
fi;
read -p "Enter GOOGLE_KEY or blank to use '$GOOGLE_KEY': " tmpGOOGLE_KEY;
if [ ! -z "$tmpGOOGLE_KEY" ]; then
GOOGLE_KEY=$tmpGOOGLE_KEY;
fi;
read -p "Enter GOOGLE_SECRET or blank to use '$GOOGLE_SECRET': " tmpGOOGLE_SECRET;
if [ ! -z "$tmpGOOGLE_SECRET" ]; then
GOOGLE_SECRET=$tmpGOOGLE_SECRET;
fi;
FileNameWithExtension=${0##*/} ;
FileNameWithoutExtension=${FileNameWithExtension%.*} ;
TimeStamp=`date "+%Y-%m-%d %r"` ;
commonPath="$(readlink -f $0 | xargs dirname)"/common;
rm -Rf ./${FileNameWithoutExtension}/ ; ## just in case one already exists.
mkdir ./${FileNameWithoutExtension}/ && cd ./${FileNameWithoutExtension}/ ;
. ${commonPath}/createApp.bsh;
## feathers generate authentication; ## all defaults
expect <(cat <<'END_OF_GENERATE_AUTH'
# generates an authentication for Google
# written from https://docs.feathersjs.com/guides/chat/authentication.html
set timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
spawn feathers generate authentication ;
expect -re ".*What authentication strategies do you want to use?.*"
##send -- "${RETURN}"
send -- " ${DOWNARROW}${DOWNARROW}${SPACE}${RETURN}"
## return defaults to Local Storage
## first space to de-select local, DOWNARROW two times, final space to select Google, and return
expect -re ".*What is the name of the user \\\(entity\\\) service.*"
send -- "${RETURN}"
## default answer is 'users'
expect -re ".*What kind of service is it?.*"
send -- "${RETURN}"
## default answer is NeDB
expect -re ".*What is the database connection string?.*"
send -- "${RETURN}"
## default answer is nedb://../data
expect eof
END_OF_GENERATE_AUTH
) ## end of feathers generate authentication
## feathers generate hook;
expect <(cat <<'END_OF_GENERATE_USER_HOOK'
# generates a "BEFORE--CREATE/UPDATE" hook named "user-hook" for the "user" service - for use with Google
# written from https://docs.feathersjs.com/api/authentication/oauth2.html#customizing-the-oauth-response
set timeout -1
set UPARROW \x1B\[A;
set DOWNARROW \x1B\[B;
set SPACE \x20;
set RETURN \x0d;
spawn feathers generate hook ;
expect -re ".*What is the name of the hook?.*"
send -- "user-hook${RETURN}"
expect -re "What kind of hook should it be?"
send -- "${DOWNARROW}${RETURN}"
## downarrow return selects 'before'
expect -re "What service\\\(s\\\) should this hook be for \\\(select none to add it yourself\\\)?"
send -- "${DOWNARROW}${SPACE}${RETURN}"
## downarrow space return selects 'users'
expect -re "What methods should the hook be for \\\(select none to add it yourself\\\)?"
send -- "${DOWNARROW}${DOWNARROW}${DOWNARROW}${SPACE}${DOWNARROW}${SPACE}${RETURN}"
## downarrow (3) space downarrow(1) space selects both 'create' and 'update'
expect eof
END_OF_GENERATE_USER_HOOK
) ## end of feathers generate hook
sed --in-place --file=- ./src/hooks/user-hook.js <<END_OF_SED_USER_HOOK ;
3i\
\\
\/* code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
const logger = require('winston');\\
const _ = require('lodash');\\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
/^ return async context => {$/ a\
\/* code added by ${FileNameWithExtension} ${TimeStamp} *\/ \\
if ( _.has(context, 'data.google.profile') ){\\
if ( _.has(context.data.google.profile, 'displayName' ) ) {\\
context.data.displayName = context.data.google.profile.displayName;\\
logger.info('displayName: ' + context.data.displayName); \\
}\\
if ( _.has(context.data.google.profile, 'emails' ) ) {\\
context.data.email = context.data.google.profile.emails.find(email => email.value).value;\\
logger.info('email: ' + context.data.email);\\
}\\
if ( _.has(context.data.google.profile, 'name' ) ) {\\
['familyName', 'givenName'].forEach( (val) => {\\
if ( _.has(context.data.google.profile.name, val ) ) {\\
context.data[val] = context.data.google.profile.name[val];\\
logger.info(val + ': ' + context.data[val]);\\
}\\
})\\
}\\
} // end if has data.google.profile\\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/\\
END_OF_SED_USER_HOOK
## modify the default.json file if you are using google (you will have to change these values!)
## values came from https://console.developers.google.com/apis/api under credidentials
sed --in-place --file=- ./config/default.json << END_OF_SED_CONFIG_DEFAULT ;
s/"host": "localhost",/"host": "${HOST}",/;
s/<google oauth key>/${GOOGLE_KEY}/;
s/"secret": "<google oauth secret>"/"secret": "${GOOGLE_SECRET}",\n" scope": [ "profile openid email"]/;
END_OF_SED_CONFIG_DEFAULT
echo " dont forget to change the IP ## in both the ./config/default.json *AND* the google-applications page (make sure they match)";
sed --in-place --file=- ./public/index.html << END_OF_INDEX_HTML ;
/<h2 class="center-text">A REST and realtime API layer for modern applications.<\/h2>/ a\
<!-- code added by ${FileNameWithoutExtension} ${TimeStamp} -->\\
<p class="center-text"><br\/>\\
<a href="/oauth/connect/google">Login With Google<\/a>\\
\\
<a href="javascript: feathersClient.logout(); Cookies.remove('feathers-jwt'); window.location.reload(true);">Log Out Of Google<\/a>\\
<br /><br />\\
Login reply will be in your console (ctrl-shift-I or right-click and select 'view console') and in the feathers console.\\
<\/p>\\
<script type="text/javascript" src="//unpkg.com/@feathersjs/client@^3.0.0/dist/feathers.js"></script>\\
<script type="text/javascript" src="//unpkg.com/[email protected]/dist/socket.io.js"></script>\\
<!-- added for ${FileNameWithExtension} ${TimeStamp} logout -->\\
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>\\
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>\\
<!-- end of added ${FileNameWithExtension} ${TimeStamp} logout -->\\
<script>\\
\\
// Socket.io is exposed as the \`io\` global.\\
var socket = io(':3030', {transports: ['websocket']});\\
// feathers-client is exposed as the \`feathers\` global.\\
var feathersClient = feathers()\\
//.configure(feathers.hooks()) commented out with 3.0\\
.configure(feathers.socketio(socket))\\
.configure(feathers.authentication({\\
cookie: 'feathers-jwt'\\
}));\\
\\
var setUserInfo = (async () => {\\
// a special thanks to feathers-slack "freeline" for graciously providing this code: \\
var response = await feathersClient.authenticate();\\
var payload = await feathersClient.passport.verifyJWT(response.accessToken);\\
var user = await feathersClient.service('users').get(payload.userId);\\
feathersClient.set('user', user); // feathersClient.get('user').email to get email address \\
console.log('you logged in as: ' + feathersClient.get('user').email); // display what is now in user in the console.\\
})\\
\\
feathersClient.authenticate()\\
.then(response => {\\
console.info('Feathers Client has Authenticated with the JWT access token!');\\
console.log(response);\\
setUserInfo();\\
})\\
.catch(error => {\\
console.info("We have not logged in with OAuth, yet. This means there's no cookie storing the accessToken. As a result, feathersClient.authenticate() failed.");\\
console.log(error);\\
});\\
</script>\\
END_OF_INDEX_HTML
cat <<END_OF_SCRIPT;
Feathers google app should now be built. Did you change your ./config/default.json values?
Client id in your ./config/default.json values file: ${GOOGLE_KEY}
Client secret in your ./config/default.json values file: ${GOOGLE_SECRET}
Now run ' cd ./${FileNameWithoutExtension}/ ; npm start; ' to try it out.
END_OF_SCRIPT
#
#