-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-hook.bsh
193 lines (152 loc) · 7.74 KB
/
github-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
#! /bin/bash
## github-hook.bsh last update: 2019-02-13
## Bash-shell script to create the Github-feathers login example and uses a hook to save the github user information.
## 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!
## values came from https://github.com/settings/developers
GITHUB_KEY='dcXXXXXXXXXXXXXfe2af' ;
GITHUB_SECRET='96039XXXXXXXXXXXXXXXXXXXXXXX6317939f3c59' ;
## get credentials here: https://github.com/settings/developers
THIS_IPADDR=$(dig +short myip.opendns.com @resolver1.opendns.com. ;);
FileNameWithExtension=${0##*/} ;
FileNameWithoutExtension=${FileNameWithExtension%.*} ;
TimeStamp=$(date "+%Y-%m-%d %r") ;
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
commonPath="$(readlink -f $0 | xargs dirname)"/common;
rm -Rf ./${FileNameWithoutExtension}/ ; ## just in case one already exists.
mkdir ./${FileNameWithoutExtension}/ && cd ./${FileNameWithoutExtension}/ ;
. ${commonPath}/createApp.bsh;
. ${commonPath}/githubNedbAuth.bsh;
## feathers generate hook;
expect <(cat <<'END_OF_GENERATE_USER_HOOK'
# generates a hook for Github
# 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 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.github.profile') ){\\
if ( _.has(context.data.github.profile, 'displayName' ) ) {\\
context.data.displayName = context.data.github.profile.displayName;\\
logger.info('displayName: ' + context.data.displayName); \\
}\\
if ( _.has(context.data.github.profile, 'familyName' ) ) {\\
context.data.displayName = context.data.github.profile.familyName;\\
logger.info('displayName: ' + context.data.familyName); \\
}\\
if ( _.has(context.data.github.profile, 'emails' ) ) {\\
context.data.email = context.data.github.profile.emails.find(email => email.value).value;\\
logger.info('email: ' + context.data.email);\\
}\\
} // end if has data.github.profile\\
\/* end of code added by ${FileNameWithExtension} ${TimeStamp} *\/\\
END_OF_SED_USER_HOOK
## modify the default.json file if you are using github (you will have to change these values!
sed --in-place --file=- ./config/default.json << END_OF_SED_CONFIG_DEFAULT ;
s/"host": "localhost",/"host": "${THIS_IPADDR}",/;
s/<github oauth key>/${GITHUB_KEY}/;
s/<github oauth secret>/${GITHUB_SECRET}/;
END_OF_SED_CONFIG_DEFAULT
echo " dont forget to change the IP ## in both the ./config/default.json *AND* the github-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\
<p class="center-text"><br\/>\\
<!-- code added by ${FileNameWithExtension} ${TimeStamp}-->\\
<a href="/oauth/connect/github">Login With GitHub<\/a>\\
\\
<a href="javascript: feathersClient.logout(); Cookies.remove('feathers-jwt'); window.location.reload(true);">Log Out Of GitHub<\/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 by FileNameWithExtension} ${TimeStamp} for 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 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 github app should now be built. Did you change your ./config/default.json values\?
Client id in your ./config/default.json values file: ${GITHUB_CLIENT_ID}
Client secret in your ./config/default.json values file: ${GITHUB_CLIENT_SECRET}
Now run ' cd ./${FileNameWithoutExtension}/ ; npm start; ' to try it out.
END_OF_SCRIPT
#
#