<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>OAuth in Action: OAuth Native Client</title>
<link href="css/style.css" rel="stylesheet">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
<header>OAuth in Action</header>
<div class="page">
<div class="block">
<p>Scope value: <br><span class="label label-danger oauth-scope-value"></span></p>
<p>Access token value: <br><span class="label label-danger oauth-access-token"></span></p>
</div>
<div class="block">
<button class="oauth-authorize" type="button">Get OAuth Token</button>
<button class="oauth-fetch-resource" type="button">Get Protected Resource</button>
</div>
<div class="block">
<div>Data from protected resource:</div>
<div>
<pre class="oauth-protected-resource"></pre>
</div>
</div>
</div>
<script>
function handleOpenURL(url) {
setTimeout(function() {
processCallback(url.substr(url.indexOf('?') + 1));
}, 0);
}
var callbackData;
// client information
var client = {
'client_name': 'native-client-1',
'client_id': '',
'client_secret': '',
'redirect_uris': ['com.oauthinaction.mynativeapp://'],
'scope': 'openid profile email address phone'
};
// authorization server information
var authServer = {
authorizationEndpoint: 'http://localhost:9001/authorize',
tokenEndpoint: 'http://localhost:9001/token',
registrationEndpoint: 'http://localhost:9001/register'
};
var protectedResource = 'http://localhost:9002/resource';
window.onload = function() {
if (!client.client_id) {
$.ajax({
url: authServer.registrationEndpoint,
type: 'POST',
data: client,
crossDomain: true,
dataType: 'json'
}).done(function(data) {
client.client_id = data.client_id;
client.client_secret = data.client_secret;
}).fail(function() {
$('.oauth-protected-resource').text('Error while fetching registration endpoint');
});
}
}
function generateState(len) {
var ret = '';
var possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (var i=0; i < len; i++) {
// add random character
ret += possible.charAt(Math.floor(Math.random() * possible.length));
}
return ret;
}
function handleAuthorizationRequestClick(ev) {
var state = generateState(32);
localStorage.setItem('oauth-state', state);
var url = authServer.authorizationEndpoint + '?' +
'response_type=code' +
'&state=' + state +
'&scope=' + encodeURIComponent(client.scope) +
'&client_id=' + encodeURIComponent(client.client_id) +
'&redirect_uri=' + encodeURIComponent(client.redirect_uris[0]);
cordova.InAppBrowser.open(url, '_system');
}
function handleFetchResourceClick(ev) {
if (callbackData != null ) {
$.ajax({
url: protectedResource,
type: 'POST',
crossDomain: true,
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + callbackData.access_token
}
}).done(function(data) {
$('.oauth-protected-resource').text(JSON.stringify(data));
}).fail(function() {
$('.oauth-protected-resource').text('Error while fetching the protected resource');
});
}
}
function processCallback(h) {
var whitelist = ['code', 'state']; // for parameters
callbackData = {};
h.split('&').forEach(function (e) {
var d = e.split('=');
if (whitelist.indexOf(d[0]) > -1) {
callbackData[d[0]] = d[1];
}
});
if (callbackData.state !== localStorage.getItem('oauth-state')) {
console.log('State DOES NOT MATCH: expected %s got %s', localStorage.getItem('oauth-state'), callbackData.state);
callbackData = null;
$('.oauth-protected-resource').text("Error state value did not match");
} else {
$.ajax({
url: authServer.tokenEndpoint,
type: 'POST',
crossDomain: true,
dataType: 'json',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
grant_type: 'authorization_code',
code: callbackData.code,
client_id: client.client_id,
client_secret: client.client_secret,
}
}).done(function(data) {
$('.oauth-access-token').text(data.access_token);
callbackData.access_token = data.access_token;
}).fail(function() {
$('.oauth-protected-resource').text('Error while getting the access token');
});
}
}
// fill placeholder on UI
$('.oauth-scope-value').text(client.scope);
// UI button click handler
$('.oauth-authorize').on('click', handleAuthorizationRequestClick);
$('.oauth-fetch-resource').on('click', handleFetchResourceClick);
</script>
</body>
</html>
没有合适的资源?快使用搜索试试~ 我知道了~
《OAuth 2实战》代码.zip
共614个文件
html:378个
js:171个
json:34个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 4 下载量 87 浏览量
2021-01-15
14:10:52
上传
评论
收藏 2.25MB ZIP 举报
温馨提示
《OAuth 2实战》代码.zip
资源推荐
资源详情
资源评论
收起资源包目录
《OAuth 2实战》代码.zip (614个子文件)
style.css 579B
style.css 579B
style.css 579B
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.DS_Store 6KB
.gitignore 149B
.gitignore 35B
.gitignore 19B
.gitignore 19B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
.gitignore 13B
index.html 6KB
index-completed.html 6KB
callback.html 5KB
callback.html 5KB
callback.html 5KB
callback.html 5KB
index.html 5KB
index.html 5KB
callback.html 5KB
callback.html 5KB
index.html 5KB
index.html 5KB
index.html 5KB
index.html 3KB
approve.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
words.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
approve.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
index.html 3KB
approve.html 2KB
approve.html 2KB
index.html 2KB
index.html 2KB
index.html 2KB
userinfo.html 2KB
index.html 2KB
index.html 2KB
共 614 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7
资源评论
- 强子@1232021-07-09用户下载后在一定时间内未进行评价,系统默认好评。
- weixin_401855902021-12-21用户下载后在一定时间内未进行评价,系统默认好评。
- qq_207736792021-09-08用户下载后在一定时间内未进行评价,系统默认好评。
- luodragon2021-03-27用户下载后在一定时间内未进行评价,系统默认好评。
Liu_Dag
- 粉丝: 37
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功