-
Notifications
You must be signed in to change notification settings - Fork 2
/
README.html
280 lines (248 loc) · 11.4 KB
/
README.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html><html><head><meta charset="utf-8"><style>html { font-size: 100%; overflow-y: scroll; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
body{
color:#444;
font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Roman',
"Hiragino Sans GB", "STXihei", "微软雅黑", serif;
font-size:12px;
line-height:1.5em;
background:#fefefe;
width: 45em;
margin: 10px auto;
padding: 1em;
outline: 1300px solid #FAFAFA;
}
a{ color: #0645ad; text-decoration:none;}
a:visited{ color: #0b0080; }
a:hover{ color: #06e; }
a:active{ color:#faa700; }
a:focus{ outline: thin dotted; }
a:hover, a:active{ outline: 0; }
span.backtick {
border:1px solid #EAEAEA;
border-radius:3px;
background:#F8F8F8;
padding:0 3px 0 3px;
}
::-moz-selection{background:rgba(255,255,0,0.3);color:#000}
::selection{background:rgba(255,255,0,0.3);color:#000}
a::-moz-selection{background:rgba(255,255,0,0.3);color:#0645ad}
a::selection{background:rgba(255,255,0,0.3);color:#0645ad}
p{
margin:1em 0;
}
img{
max-width:100%;
}
h1,h2,h3,h4,h5,h6{
font-weight:normal;
color:#111;
line-height:1em;
}
h4,h5,h6{ font-weight: bold; }
h1{ font-size:2.5em; }
h2{ font-size:2em; border-bottom:1px solid silver; padding-bottom: 5px; }
h3{ font-size:1.5em; }
h4{ font-size:1.2em; }
h5{ font-size:1em; }
h6{ font-size:0.9em; }
blockquote{
color:#666666;
margin:0;
padding-left: 3em;
border-left: 0.5em #EEE solid;
}
hr { display: block; height: 2px; border: 0; border-top: 1px solid #aaa;border-bottom: 1px solid #eee; margin: 1em 0; padding: 0; }
pre , code, kbd, samp {
color: #000;
font-family: monospace;
font-size: 0.88em;
border-radius:3px;
background-color: #F8F8F8;
border: 1px solid #CCC;
}
pre { white-space: pre; white-space: pre-wrap; word-wrap: break-word; padding: 5px 12px;}
pre code { border: 0px !important; padding: 0;}
code { padding: 0 3px 0 3px; }
b, strong { font-weight: bold; }
dfn { font-style: italic; }
ins { background: #ff9; color: #000; text-decoration: none; }
mark { background: #ff0; color: #000; font-style: italic; font-weight: bold; }
sub, sup { font-size: 75%; line-height: 0; position: relative; vertical-align: baseline; }
sup { top: -0.5em; }
sub { bottom: -0.25em; }
ul, ol { margin: 1em 0; padding: 0 0 0 2em; }
li p:last-child { margin:0 }
dd { margin: 0 0 0 2em; }
img { border: 0; -ms-interpolation-mode: bicubic; vertical-align: middle; }
table { border-collapse: collapse; border-spacing: 0; }
td { vertical-align: top; }
@media only screen and (min-width: 480px) {
body{font-size:14px;}
}
@media only screen and (min-width: 768px) {
body{font-size:16px;}
}
@media print {
* { background: transparent !important; color: black !important; filter:none !important; -ms-filter: none !important; }
body{font-size:12pt; max-width:100%; outline:none;}
a, a:visited { text-decoration: underline; }
hr { height: 1px; border:0; border-bottom:1px solid black; }
a[href]:after { content: " (" attr(href) ")"; }
abbr[title]:after { content: " (" attr(title) ")"; }
.ir a:after, a[href^="javascript:"]:after, a[href^="#"]:after { content: ""; }
pre, blockquote { border: 1px solid #999; padding-right: 1em; page-break-inside: avoid; }
tr, img { page-break-inside: avoid; }
img { max-width: 100% !important; }
@page :left { margin: 15mm 20mm 15mm 10mm; }
@page :right { margin: 15mm 10mm 15mm 20mm; }
p, h2, h3 { orphans: 3; widows: 3; }
h2, h3 { page-break-after: avoid; }
}
</style><title>HOWTO</title></head><body><p><strong>Contents:</strong></p>
<div class="toc">
<ul>
<li><a href="#auth-sdk">auth-sdk</a><ul>
<li><a href="#include-with-composer">Include with composer</a></li>
<li><a href="#oauth2-web-grant-type-usage">Oauth2 web grant type usage</a><ul>
<li><a href="#create-the-sdk-object">Create the sdk object</a></li>
<li><a href="#parse-the-response-login-redirected-back-to-your-site">Parse the response (login redirected back to your site)</a></li>
<li><a href="#check-user-status-show-login-or-logout">Check user status. Show login or logout</a></li>
</ul>
</li>
<li><a href="#examples">Examples</a><ul>
<li><a href="#running">Running</a></li>
<li><a href="#files">Files</a></li>
<li><a href="#config">Config</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div style="page-break-after: always;"></div>
<h1 id="auth-sdk">auth-sdk</h1>
<p>The auth-sdk is just a simple wrapper around persistent state storage and redirect response parsing for the
<a href="http://tools.ietf.org/html/rfc6749#section-4.1">OAuth2 web grant type</a>.</p>
<p>A simpler explanation is given <a href="http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified">http://aaronparecki.com/articles/2012/07/29/1/oauth2-simplified</a>.
You should read it at least once, to have a basic understanding of OAuth2.
This sdk covers the "Web Server Apps" part mentioned there.</p>
<p>The auth-sdk hides most of the OAuth2 stuff, so you'll basically have to do 3 things (in this order):</p>
<ul>
<li>First try to parse the redirect sent back to your site after the login.<ul>
<li>Call sdk->parseRedirectResponse()</li>
</ul>
</li>
<li>Check, if you have permissions (=access token) to call an api method for the user.<ul>
<li>Call sdk->getUser()</li>
</ul>
</li>
<li>If not, redirect the user to an external login page. <sup id="fnref:1"><a class="footnote-ref" href="#fn:1" rel="footnote">1</a></sup><ul>
<li>Redirect to sdk->getLoginUrl()</li>
</ul>
</li>
</ul>
<p>Your app now should have permissions to make an api call on behalf of the user. Take a look at: <code>./examples/basic.php</code></p>
<p>You can use the acces token then stored in the auth-sdk for subsequent api calls until the token expires.
(Server-side or until the user logs out)</p>
<h2 id="include-with-composer">Include with composer</h2>
<pre><code>
"require": {
"aboutyou/auth-sdk": "~0.3.6"
}
</code></pre>
<p><strong>If you just want to run the examples now, please refer to the <a href="#examples2">"Examples"</a> section.</strong></p>
<div style="page-break-after: always;"></div>
<h2 id="oauth2-web-grant-type-usage">Oauth2 web grant type usage</h2>
<ul>
<li>Have a look at: <code>./examples/basic.php</code></li>
</ul>
<h3 id="create-the-sdk-object">Create the sdk object</h3>
<pre><code>$authSDK = new AuthSDK(array(
'clientId'=>'from_dev_center',
'clientToken'=>'from_dev_center',
'clientSecret' => 'from_dev_center',
'redirectUri'=>'entered_in_dev_center',
'scope'=>'firstname', //optional. the only valid value is firstname at the moment
'popup'=>'true|false', //optional. true will render a small login, false the login webpage
),new SessionStorage());
</code></pre>
<p>(There are two optional parameters that can be "overwritten" 'loginUrl' and 'resourceUrl'. But usually there is no need
for that)</p>
<h3 id="parse-the-response-login-redirected-back-to-your-site">Parse the response (login redirected back to your site)</h3>
<ul>
<li>Call this method first and only once per request</li>
<li>$parsed will be true, if it was a redirect. Just in case you need this information.</li>
</ul>
<pre><code>$parsed = $authSDK->parseRedirectResponse();
</code></pre>
<h3 id="check-user-status-show-login-or-logout">Check user status. Show login or logout</h3>
<ul>
<li>Check, if login button|redirect needed</li>
</ul>
<pre><code>$authResult = $authSDK->getUser();
if($authResult->hasErrors()){
//optional, add values you want to get back on your redirect endpoint
//but do this before getLoginUrl()
$authSDK->setState('someKey','someVal');
renderLoginButton( $authSDK->getLoginUrl() ); //renderLoginButton: your render method
var_dump($authResult->getErrors());
}else{
renderLogoutButton( $authSDK->getLogoutUrl() ); //renderLogoutButton: your render method
var_dump($authResult->getResult()->response);
var_dump($authSDK->getState('someKey'));
}
</code></pre>
<ul>
<li>Its also possible to set 'state' params (will be returned)</li>
<li>getLogoutUrl() has an optional parameter redirectUrl. If set, after the logout you will be redirected to that. Else to the 'redirectUri' of the sdk config.</li>
<li>In both cases getLogoutUrl() will append the get parameter logout=true to redirectUrl (which is used by e.g. parseRedirectUrl() to clean the persistent storage).
<div style="page-break-after: always;"></div></li>
</ul>
<div id="examples2"></div>
<h2 id="examples">Examples</h2>
<ul>
<li>Can be found in the folder: <code>./examples/*</code></li>
</ul>
<h3 id="running">Running</h3>
<ul>
<li>Open a terminal, in the sdk root folder run <code>php composer.phar install</code>.
<hr> </li>
<li>If you use php >= 5.4 you can now try <code>./run.sh</code> (as root/sudo) in the <code>./examples</code> folder. Keep an eye on the console messages (it will complain about the /etc/hosts config, add it). If everything works, you should be able to open the (local) url shown in the console afterwards.
<hr></li>
<li>On Windows, or if run.sh is not working: With php >= 5.4 just try <code>php -S mytestserver.local:8080</code> in the <code>./examples</code> folder (as root/sudo on linux/osx). Notice you have to add <code>127.0.0.1 mytestserver.local</code> to your <code>/etc/hosts</code> for this to work. (Windows: system32\drivers\etc\hosts)
<hr></li>
<li>For php < 5.4 refer to your server documentation to create a vhost config with the document root pointing to the <code>./examples</code> folder.</li>
</ul>
<h3 id="files">Files</h3>
<ul>
<li>
<p><code>./examples/basic.php</code>:</p>
<ul>
<li>example for typical usecase, just as much code as needed. Note, that you need to set your redirectUrl to {yourdomain}/basic.php</li>
</ul>
</li>
<li>
<p><code>./examples/parent_page.php</code> & <code>./examples/result_page.php</code>:</p>
<ul>
<li>example for showing more options, intercepts the redirect, so you can take a look at the params returned. Note, that you need to set your redirectUrl to {yourdomain}/result_page.php</li>
</ul>
</li>
</ul>
<h3 id="config">Config</h3>
<ul>
<li>Copy <code>./example/common_params.php</code> to <code>./example/common_params.local.php</code></li>
<li>Change the params in ./example/common_params.local.php to match your values (from dev center)</li>
</ul>
<div class="footnote">
<hr />
<ol>
<li id="fn:1">
<ul>
<li>If the user is not logged in there, it will grant the user for its username and password and then redirect back to your site with an access token.</li>
<li>If your user however already is logged in, it will just redirect back to your site with an access token.</li>
<li>There is one more authorization flow step after those possible grants from the user and really fetching the access token, but the auth-sdk will gently hide that from you</li>
</ul>
<p><a class="footnote-backref" href="#fnref:1" rev="footnote" title="Jump back to footnote 1 in the text">↩</a></p>
</li>
</ol>
</div></body></html>