-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_oauth.php
216 lines (182 loc) · 6.36 KB
/
test_oauth.php
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
<?php
require __DIR__ . '/vendor/autoload.php';
$name = 'MyMastodonApp';
$instance = 'mastodon.social';
$oAuth = new Colorfield\Mastodon\MastodonOAuth($name, $instance);
$authorizationUrl = $oAuth->getAuthorizationUrl();
// @todo set php wrappers to be called from js
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Test Mastodon API | oAuth</title>
<!-- Bootstrap core CSS -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<style type="text/css">
body {
padding-top: 2rem;
padding-bottom: 2rem;
}
h3 {
margin-top: 2rem;
}
.row {
margin-bottom: 1rem;
}
.row .row {
margin-top: 1rem;
margin-bottom: 0;
}
[class*="col-"] {
padding-top: 1rem;
padding-bottom: 1rem;
background-color: rgba(86, 61, 124, .15);
border: 1px solid rgba(86, 61, 124, .2);
}
hr {
margin-top: 2rem;
margin-bottom: 2rem;
}
</style>
<script type="text/javascript">
$(document).ready(function() {
function getToken() {
var authData = {
'grant_type': "authorization_code",
'redirect_uri': "urn:ietf:wg:oauth:2.0:oob",
'client_id': $("#client_id").text(),
'client_secret': $("#client_secret").text(),
'code': $('input[name=auth-code]').val()
};
console.log(authData);
$('#bearer').html('sending...');
// process the form
$.ajax({
type : 'POST',
url : 'https://<?php echo $instance; ?>/oauth/token',
data : authData,
dataType : 'json',
encode : true
})
// using the done promise callback
.done(function(data) {
if(data.error) {
$('#bearer').html(data.error_description);
}else{
$('#bearer').html(data.access_token);
}
});
}
function login() {
var authData = {
'grant_type': "password",
'client_id': $("#client_id").text(),
'client_secret': $("#client_secret").text(),
'username': $('input[name=email]').val(),
'password': $('input[name=password]').val(),
'scope': 'read write'
};
console.log(authData);
$('#login-result').html('sending...');
// process the form
$.ajax({
type : 'POST',
url : 'https://<?php echo $instance; ?>/oauth/token',
data : authData,
dataType : 'json',
encode : true
})
// using the done promise callback
.done(function(data) {
if(data.error) {
$('#login-result').html(data.error_description);
}else{
$('#login-result').html('Login OK - Bearer:' + data.access_token);
}
});
}
// process the forms
$('.get-access-token').on('click', function(e) {
e.preventDefault();
getToken();
});
$('.login').on('click', function(e) {
e.preventDefault();
login();
});
});
</script>
</head>
<body>
<div class="container">
<h1>Test Mastodon API</h1>
<h2>Authenticate</h2>
<h4>1. Click on authorize and get the auth code</h4>
<div class="row">
<div class="col-md-4">client_id</div>
<div class="col-md-8" id="client_id"><?php echo $oAuth->config->getClientId(); ?></div>
</div>
<div class="row">
<div class="col-md-4">client_secret</div>
<div class="col-md-8" id="client_secret"><?php echo $oAuth->config->getClientSecret(); ?></div>
</div>
<div class="row">
<div class="col-md-4">authorization_url</div>
<div class="col-md-8" id="authorization_url"><a href="<?php echo $authorizationUrl ?>"
target="_blank">Authorize</a></div>
</div>
<h4>2. Paste the auth code in this form then submit</h4>
<form id="access-token">
<div class="form-group row">
<div class="col-md-4">
<label for="auth-code">Auth code</label>
</div>
<div class="col-md-8">
<input class="form-control" type="text" value="" placeholder="Paste the code" name="auth-code">
</div>
</div>
<button type="submit" class="btn btn-primary get-access-token">Get access token</button>
</form>
<div class="row"></div>
<h4>3. Login with the bearer + email and password</h4>
<div class="row">
<div class="col-md-4">bearer</div>
<div class="col-md-8" id="bearer"></div>
</div>
<form id="login">
<div class="form-group row">
<div class="col-md-4">
<label for="auth-code">Email</label>
</div>
<div class="col-md-8">
<input class="form-control" type="email" value="" placeholder="Email" name="email">
</div>
<div class="col-md-4">
<label for="auth-code">Password</label>
</div>
<div class="col-md-8">
<input class="form-control" type="password" value="" placeholder="Password" name="password">
</div>
</div>
<button type="submit" class="btn btn-primary login">Login</button>
</form>
<div class="row"></div>
<div class="row">
<div class="col-md-4">login result</div>
<div class="col-md-8" id="login-result"></div>
</div>
<h2>Use the API wrapper</h2>
<p>Go to <em><a href="test_api.php">test_api.php</a></em> in your browser after having defined the <em>test_credentials.php</em> file.</a></p>
</div> <!-- /container -->
</body>
</html>