-
Notifications
You must be signed in to change notification settings - Fork 5
/
github-auth.html
117 lines (94 loc) · 4.67 KB
/
github-auth.html
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
<!DOCTYPE html>
<html>
<head>
<title>github-auth.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>github-auth.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre>
<span class="hljs-keyword">var</span> GitHubStrategy = <span class="hljs-built_in">require</span>(<span class="hljs-string">'passport-github'</span>).Strategy
<span class="hljs-keyword">var</span> _ = <span class="hljs-built_in">require</span>(<span class="hljs-string">'lodash'</span>)
<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">options</span>) </span>{
<span class="hljs-keyword">var</span> seneca = <span class="hljs-keyword">this</span>
<span class="hljs-keyword">var</span> service = <span class="hljs-string">'github'</span>
<span class="hljs-keyword">var</span> params = {
clientID: options.clientID,
clientSecret: options.clientSecret,
callbackURL: options.urlhost + (options.callbackUrl || <span class="hljs-string">'/auth/github/callback'</span>)
}
params = _.extend(params, options.serviceParams || {})
<span class="hljs-keyword">var</span> authPlugin = <span class="hljs-keyword">new</span> GitHubStrategy(params,
<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">accessToken, refreshToken, profile, done</span>) </span>{
seneca.act(
{
role: <span class="hljs-string">'auth'</span>,
prepare: <span class="hljs-string">'github_login_data'</span>,
accessToken: accessToken,
refreshToken: refreshToken,
profile: profile
}, done)
}
)
<span class="hljs-keyword">var</span> prepareLoginData = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">args, cb</span>) </span>{
<span class="hljs-keyword">var</span> accessToken = args.accessToken
<span class="hljs-keyword">var</span> refreshToken = args.refreshToken
<span class="hljs-keyword">var</span> profile = args.profile
<span class="hljs-keyword">var</span> data = {
identifier: <span class="hljs-string">''</span> + profile.id,
nick: profile.username,
username: profile.username,
credentials: {
access: accessToken,
refresh: refreshToken
},
userdata: profile,
when: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toISOString()
}
data = _.extend({}, data, profile)
<span class="hljs-keyword">if</span> (data.emails && data.emails.length > <span class="hljs-number">0</span> && data.emails[<span class="hljs-number">0</span>].value) {
data.email = data.emails[<span class="hljs-number">0</span>].value
data.nick = data.email
}
<span class="hljs-keyword">if</span> (data.name && _.isObject(data.name)) {
data.firstName = data.name.givenName
data.lastName = data.name.familyName
data.name = data.name || (data.firstName + <span class="hljs-string">' '</span> + data.lastName)
<span class="hljs-keyword">delete</span> data.name
}
data.name = data.name || data.displayName
data[ service + <span class="hljs-string">'_id'</span> ] = data.identifier
data.service = data.service || {}
data.service[ service ] = {
credentials: data.credentials,
userdata: data.userdata,
when: data.when
}
cb(<span class="hljs-literal">null</span>, data)
}
seneca.add({role: <span class="hljs-string">'auth'</span>, prepare: <span class="hljs-string">'github_login_data'</span>}, prepareLoginData)
seneca.act({role: <span class="hljs-string">'auth'</span>, cmd: <span class="hljs-string">'register_service'</span>, service: service, plugin: authPlugin, conf: options})
<span class="hljs-keyword">return</span> {
name: <span class="hljs-string">'github-auth'</span>
}
}</pre></div></div>
</li>
</ul>
</div>
</body>
</html>