-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.html
196 lines (172 loc) · 8.86 KB
/
oauth.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="ext/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" charset="utf-8" src="ext/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="ext/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="ext/cookie.js"></script>
<link href="css/style.css" rel="stylesheet" media="screen">
<title>OAuth 2 Implicit Grant User Agent Authentication Flow Demo</title>
<script type="text/javascript" charset="utf-8">
$(function () {
var extractResponse = function (location, defaultClientId) {
var result = {
token: null,
error: null,
client: null};
// clientID to use
var cMatch = location.hash.match(/client=([\w\-^/^=]+)/);
if (cMatch) {
result.client = cMatch[1];
} else {
var clientCookie = docCookies.getItem('client');
if (clientCookie && clientCookie !== 'null') {
result.client = clientCookie;
} else {
result.client = defaultClientId;
}
}
docCookies.setItem('client', result.client, null, location.pathname);
// token if we where already authorized
var atMatch = location.hash.match(/at=(\w+)/);
if (atMatch) {
result.token = atMatch[1];
}
// token from auth server
var tokenMatch = location.hash.match(/access_token=(\w+)/);
if (tokenMatch) {
result.token = tokenMatch[1];
}
var errorMatch = location.search.match(/error=(\w+)/);
if (errorMatch) {
result.error = errorMatch[1];
}
return result;
};
var cleanLocation = function (at, client) {
if ('replaceState' in history) {
var loc = location.pathname;
if (at || client) {
loc += '#';
}
loc += getURLHashEncodedParams(at, client);
history.replaceState('', document.title, loc);
} else {
location.hash = getURLHashEncodedParams(at, client);
location.search = '';
}
return location.href.split('?')[0].split('#')[0];
};
var getURLHashEncodedParams = function(at, client) {
var loc = '';
var separator = false;
if (at) {
loc += 'at=' + at;
separator = true;
}
if (client) {
if (separator) {
loc += '/'
}
loc += 'client=' + client;
}
return loc;
};
var loadResource = function (resourceHost, accessToken, successCallback, errorCallback) {
$('#loadResource').button('loading');
// access the OAuth protected resource
// this requires a CORS enabled browser and server...
$.ajax({
url: resourceHost,
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Bearer " + accessToken);
xhr.setRequestHeader('Accept', "application/json");
},
complete: function () {
$('#loadResource').button('reset');
}, success: successCallback, error: errorCallback
});
};
var loadResourceSuccess = function (response) {
if (response) {
$('#resourceContent').text(JSON.stringify(response));
} else {
$('#resourceContent').text("An error occurred.");
}
};
var loadResourceError = function (jqXHR, textStatus, errorThrown) {
$('#alert').addClass('alert-danger').html('<strong>Error</strong> accessing resource server: (HTTP ' + jqXHR.status + ' ' + errorThrown + ')');
};
$('#loadResource').click(function (event) {
loadResource(resourceHost, response.token, loadResourceSuccess, loadResourceError);
$('#alert').removeClass('alert-danger').text('');
});
//mm-test-espresso-2
//Client-ID 8541142e-b205-4b82-9c79-3a4c4f0f4f96
//redirect = 'http://localhost/oauth/oauth.html'
// test-2
//2815f74a-17fc-4b92-977e-62a2a0a5d181
// auth settings (default clientId)
var defaultClientId = '8541142e-b205-4b82-9c79-3a4c4f0f4f96';
var authHost = 'authserver-j1bof3861.rhcloud.com';
var endUserAuthorizationEndpoint = 'http://' + authHost + "/oauth2/authorize";
var scope = 'espresso';
// resource settings
var resourceHost = 'http://resourceserver-j1bof3861.rhcloud.com/rest/coffee';
// process auth server response
var response = extractResponse(location, defaultClientId);
// beautify locationbar and get correct redirect uri
var redirect = cleanLocation(response.token, response.client);
if (response.token) {
$('#authenticated').removeClass('hide');
$('#token').text(response.token);
loadResource(resourceHost, response.token, loadResourceSuccess, loadResourceError);
} else if (response.error) {
$('#alert').addClass('alert-danger').text('Authentication error: ' + response.error);
}
var authUrl = endUserAuthorizationEndpoint +
"?response_type=token" +
"&client_id=" + response.client +
"&redirect_uri=" + redirect +
"&scope=" + scope;
$('#connect').attr("href", authUrl);
$('#authHost').text(authHost);
$('#clientId').text(response.client);
if (response.client === defaultClientId) {
$('#defaultClient').removeClass('hide');
}
}
);
</script>
</head>
<body>
<div class="container">
<h1>OAuth 2 Implicit Grant User Agent Authentication Flow Demo</h1>
<div id="authenticate">
<h3>Authenticate</h3>
<a id="connect" class="btn btn-primary" href="">Connect</a> <strong>Provider</strong> <span id="authHost"></span> <strong>Client</strong> <span id="clientId"></span>
<span id="defaultClient" class="hide"><strong>(default)</strong></span>
</div>
<div id="authenticated" class="hide">
<h3>Authenticated Successful</h3>
<p>
You are using access token <span id="token">[no token]</span>
</p>
<h4>Resource Content</h4>
<p>
<button type="button" class="btn btn-primary" id="loadResource"
data-loading-text="<i class='glyphicon glyphicon-refresh spin'></i> refreshing...">
refresh
</button>
</p>
<p>
<span id="resourceContent">[no resource content]</span>
</p>
</div>
<div id="alert" class="alert">
</div>
</div>
</body>
</html>