nativescript-oauth2
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -33,2 +33,3 @@ /// <reference path="./providers/providers.d.ts" /> | ||
loginWithCompletion(completion?: TnsOAuthClientLoginBlock): void; | ||
refreshTokenWithCompletion(completion?: TnsOAuthClientLoginBlock): void; | ||
resumeWithUrl(url: string): void; | ||
@@ -35,0 +36,0 @@ logout(successPage?: string): void; |
19
oauth.js
@@ -38,2 +38,10 @@ "use strict"; | ||
}; | ||
TnsOAuthClient.prototype.refreshTokenWithCompletion = function (completion) { | ||
if (this.provider) { | ||
this.callRefreshEndpointWithCompletion(completion); | ||
} | ||
else { | ||
completion(null, "Provider is not configured"); | ||
} | ||
}; | ||
TnsOAuthClient.prototype.logout = function (successPage) { | ||
@@ -115,2 +123,13 @@ this.removeCookies(); | ||
}; | ||
TnsOAuthClient.prototype.callRefreshEndpointWithCompletion = function (completion) { | ||
var _this = this; | ||
if (!this.provider.tokenEndpoint) { | ||
return; | ||
} | ||
var connection = tns_oauth_client_connection_1.TnsOAuthClientConnection.initWithRequestClientCompletion(this, function (data, result, error) { | ||
_this.tokenResult = tns_oauth_utils_1.httpResponseToToken(result); | ||
completion(_this.tokenResult, error); | ||
}); | ||
connection.startTokenRefresh(); | ||
}; | ||
return TnsOAuthClient; | ||
@@ -117,0 +136,0 @@ }()); |
{ | ||
"name": "nativescript-oauth2", | ||
"version": "1.2.0", | ||
"description": "OAuth 2 login for NativeScript.", | ||
"version": "1.3.0", | ||
"description": "OAuth 2 generic authorization plugin for NativeScript that doesn't install third party native libraries", | ||
"main": "oauth", | ||
@@ -66,4 +66,4 @@ "typings": "index.d.ts", | ||
"devDependencies": { | ||
"tns-core-modules": "^5.0.2", | ||
"tns-platform-declarations": "^5.0.2", | ||
"tns-core-modules": "^5.1.1", | ||
"tns-platform-declarations": "^5.1.1", | ||
"typescript": "~2.8.2", | ||
@@ -70,0 +70,0 @@ "prompt": "^1.0.0", |
@@ -195,2 +195,27 @@ # OAuth 2 Plugin for NativeScript | ||
### Refreshing the Access Token | ||
Once you have logged in, you can call `refreshTokenWithCompletion()` on your `TnsOAuthClient` instance to attempt to refresh your access token. In order to do this, the following criteria must be met: | ||
* The scope `offline_access` was requested when you logged in. | ||
* The `TnsOAuthClient` must have the token result from your previous login. If you have the original instance you used to log in, it will already be on the object. If you do not have the original instance of `TnsOAuthClient` which you used to log in, such as if the app was restarted, then assign the client's `tokenResult` property to your token. | ||
If that criteria is met, then you can refresh the token like so: | ||
``` | ||
import { TnsOAuthClient, ITnsOAuthTokenResult } from "nativescript-oauth2"; | ||
... | ||
client.refreshTokenWithCompletion((tokenResult: ITnsOAuthTokenResult, error) => { | ||
if (error) { | ||
console.error("Unable to refresh token with error: "); | ||
console.error(error); | ||
} else { | ||
console.log("Successfully refreshed access token: "); | ||
console.log(tokenResult); | ||
} | ||
}); | ||
``` | ||
### Creating a custom provider | ||
@@ -197,0 +222,0 @@ |
@@ -6,2 +6,3 @@ "use strict"; | ||
var http = require("tns-core-modules/http"); | ||
var tns_oauth_utils_1 = require("./tns-oauth-utils"); | ||
var accessTokenName = "access_token"; | ||
@@ -64,2 +65,43 @@ var TnsOAuthClientConnection = (function () { | ||
}; | ||
TnsOAuthClientConnection.prototype.startTokenRefresh = function () { | ||
var _this = this; | ||
var tokenUrl = this.client.provider.authority + this.client.provider.tokenEndpoint; | ||
var headers = { | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
}; | ||
var body = null; | ||
switch (this.client.provider.options.openIdSupport) { | ||
case "oid-full": | ||
var options1 = this.client.provider.options; | ||
body = querystring.stringify({ | ||
grant_type: "refresh_token", | ||
refresh_token: this.client.tokenResult.refreshToken, | ||
client_id: options1.clientId | ||
}); | ||
break; | ||
case "oid-none": | ||
var options2 = this.client.provider.options; | ||
body = querystring.stringify({ | ||
grant_type: "refresh_token", | ||
refresh_token: this.client.tokenResult.refreshToken, | ||
client_id: options2.clientId, | ||
client_secret: options2.clientSecret | ||
}); | ||
} | ||
http | ||
.request({ | ||
url: tokenUrl, | ||
method: "POST", | ||
headers: headers, | ||
content: body | ||
}) | ||
.then(function (response) { | ||
if (response.statusCode !== 200) { | ||
_this.completion(null, response, new Error("Failed refresh token with status " + response.statusCode + ".")); | ||
} | ||
else { | ||
_this.completion(null, response, null); | ||
} | ||
}); | ||
}; | ||
TnsOAuthClientConnection.prototype.getTokenFromCode = function (client, code, completion) { | ||
@@ -133,22 +175,3 @@ var oauthParams = { | ||
.then(function (response) { | ||
var results; | ||
try { | ||
results = response.content.toJSON(); | ||
} | ||
catch (e) { | ||
results = querystring.parse(response.content.toString()); | ||
} | ||
var access_token = results["access_token"]; | ||
var refresh_token = results["refresh_token"]; | ||
var expires_in = results["expires_in"]; | ||
delete results["refresh_token"]; | ||
var expSecs = Math.floor(parseFloat(expires_in)); | ||
var expDate = new Date(); | ||
expDate.setSeconds(expDate.getSeconds() + expSecs); | ||
var tokenResult = { | ||
accessToken: access_token, | ||
refreshToken: refresh_token, | ||
accessTokenExpiration: expDate, | ||
refreshTokenExpiration: expDate | ||
}; | ||
var tokenResult = tns_oauth_utils_1.httpResponseToToken(response); | ||
completion(tokenResult, response); | ||
@@ -155,0 +178,0 @@ resolve(response); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var web_view_1 = require("tns-core-modules/ui/web-view"); | ||
var application_1 = require("tns-core-modules/application"); | ||
var page_1 = require("tns-core-modules/ui/page"); | ||
@@ -8,2 +9,3 @@ var grid_layout_1 = require("tns-core-modules/ui/layouts/grid-layout"); | ||
var tns_oauth_login_sub_controller_1 = require("./tns-oauth-login-sub-controller"); | ||
var SOFT_INPUT_ADJUST_RESIZE = 16; | ||
var TnsOAuthLoginWebViewController = (function () { | ||
@@ -32,2 +34,3 @@ function TnsOAuthLoginWebViewController() { | ||
TnsOAuthLoginWebViewController.prototype.createWebViewPage = function (url) { | ||
var _this = this; | ||
var webView = this.createWebView(url, this.pageLoadStarted.bind(this), this.pageLoadFinished.bind(this)); | ||
@@ -38,5 +41,23 @@ var grid = new grid_layout_1.GridLayout(); | ||
page.content = grid; | ||
var navBtn = new action_bar_1.NavigationButton(); | ||
navBtn.text = "Done"; | ||
page.actionBar.navigationButton = navBtn; | ||
if (page_1.isAndroid) { | ||
page.actionBarHidden = true; | ||
page.on("navigatedTo", function () { | ||
_this.setAndroidSoftInputModeToResize(); | ||
webView.android.getSettings().setDomStorageEnabled(true); | ||
webView.android.getSettings().setBuiltInZoomControls(false); | ||
}); | ||
page.on("navigatingFrom", function () { | ||
_this.restoreAndroidSoftInputMode(); | ||
}); | ||
} | ||
else { | ||
var navBtn = new action_bar_1.NavigationButton(); | ||
navBtn.text = ""; | ||
page.actionBar.navigationButton = navBtn; | ||
} | ||
var onCancel = function () { | ||
_this.loginController.completeLoginWithTokenResponseError(null, new Error("User cancelled.")); | ||
}; | ||
page.on("navigatedFrom", onCancel); | ||
this.unbindCancelEvent = function () { return page.off("navigatedFrom", onCancel); }; | ||
return page; | ||
@@ -55,2 +76,5 @@ }; | ||
_this.loginController.completeLoginWithTokenResponseError(tokenResult, error); | ||
if (_this.unbindCancelEvent) { | ||
_this.unbindCancelEvent(); | ||
} | ||
_this.loginController.frame.goBack(); | ||
@@ -68,2 +92,11 @@ }); | ||
}; | ||
TnsOAuthLoginWebViewController.prototype.setAndroidSoftInputModeToResize = function () { | ||
var window = application_1.android.foregroundActivity.getWindow(); | ||
this.originalSoftInputMode = window.getAttributes().softInputMode; | ||
window.setSoftInputMode(SOFT_INPUT_ADJUST_RESIZE); | ||
}; | ||
TnsOAuthLoginWebViewController.prototype.restoreAndroidSoftInputMode = function () { | ||
var window = application_1.android.foregroundActivity.getWindow(); | ||
window.setSoftInputMode(this.originalSoftInputMode); | ||
}; | ||
return TnsOAuthLoginWebViewController; | ||
@@ -70,0 +103,0 @@ }()); |
@@ -13,3 +13,3 @@ "use strict"; | ||
params["redirect_uri"] = provider.options.redirectUri; | ||
params["scope"] = provider.options.scopes.join(' '); | ||
params["scope"] = provider.options.scopes && provider.options.scopes.join(' '); | ||
params["response_mode"] = "query"; | ||
@@ -51,3 +51,3 @@ params["state"] = "abcd"; | ||
params["client_secret"] = provider.options.clientSecret; | ||
params["scope"] = provider.options.scopes.join(' '); | ||
params["scope"] = provider.options.scopes && provider.options.scopes.join(' '); | ||
params["state"] = "abcd"; | ||
@@ -97,2 +97,25 @@ var pararmsStr = querystring.stringify(params); | ||
exports.jsArrayToNSArray = jsArrayToNSArray; | ||
function httpResponseToToken(response) { | ||
var results; | ||
try { | ||
results = response.content.toJSON(); | ||
} | ||
catch (e) { | ||
results = querystring.parse(response.content.toString()); | ||
} | ||
var access_token = results["access_token"]; | ||
var refresh_token = results["refresh_token"]; | ||
var expires_in = results["expires_in"]; | ||
delete results["refresh_token"]; | ||
var expSecs = Math.floor(parseFloat(expires_in)); | ||
var expDate = new Date(); | ||
expDate.setSeconds(expDate.getSeconds() + expSecs); | ||
return { | ||
accessToken: access_token, | ||
refreshToken: refresh_token, | ||
accessTokenExpiration: expDate, | ||
refreshTokenExpiration: expDate | ||
}; | ||
} | ||
exports.httpResponseToToken = httpResponseToToken; | ||
//# sourceMappingURL=tns-oauth-utils.js.map |
101823
1132
363