Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make window-options and callback-url configurable #6

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ti.oauth",
"version": "0.3.1",
"version": "0.4.0",
"description": "OAuth 2.0 Implementation for Titanium",
"main": "./ti.oauth.js",
"author": {
Expand Down
31 changes: 13 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/* global Ti, L */
// RFC for OAuth2: https://tools.ietf.org/html/rfc6749

// TODO Do we want to support OAuth 1.0? https://github.com/indieocean/Titanium-OAuth-Client/blob/master/titanium_oauth.js
// TODO Allow users to specify scopes when authorizing
const CALLBACK_URL = 'http://localhost/Callback'; //'urn:ietf:wg:oauth:2.0:oob' // FIXME can we redirect to some other URL scheme that won't show errors in console?
// TODO Allow users to modify the look and feel of the window that opens the webview for auth? really doesn't matter much...
// Only important one here is the title.
const AUTH_WINDOW_OPTIONS = {
backgroundColor : 'white',
translucent: false,
title : 'OAuth Example'
};

const isiOS = Ti.Platform.osname == 'iphone' || Ti.Platform.osname == 'ipad';

/********** Helpers **********/
Expand Down Expand Up @@ -89,7 +78,7 @@ function parseQueryParams(url) {
keyValuePair;

// FIXME handle when there are no query params?
pairs = decodeURI(url).slice(CALLBACK_URL.length + 1).split('&'); // cut off base callback URL and ? char
pairs = decodeURI(url).slice(this.callbackUrl.length + 1).split('&'); // cut off base callback URL and ? char

for (var i = 0; i < pairs.length; i++) {
keyValuePair = pairs[i].split('=');
Expand All @@ -108,6 +97,12 @@ var OAuth = function OAuth(clientId) {
this.tokenType = null;
this.expiresIn = 0;
this.ignoreSslError = false;
this.callbackUrl = 'http://localhost/Callback'; // FIXME: We should remove this in the next major release, keeping for backwards compatibility.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks OK, but...

this.authWindowOptions = {
backgroundColor : 'white',
translucent: false,
title: 'OAuth Example'
};
};

/**
Expand Down Expand Up @@ -196,7 +191,7 @@ OAuth.authorizeImplicitly = function(url, clientId, scopes, callback) {
webview,
state = generateGUID();

win = Ti.UI.createWindow(AUTH_WINDOW_OPTIONS);
win = Ti.UI.createWindow(this.authWindowOptions);

if (isiOS === true) {
var closeButton = Ti.UI.createButton({
Expand All @@ -221,7 +216,7 @@ OAuth.authorizeImplicitly = function(url, clientId, scopes, callback) {
url : buildURL(url, {
scope: scopes,
approval_prompt: 'force',
redirect_uri: CALLBACK_URL,
redirect_uri: this.callbackUrl,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...here it's undefined!

response_type: 'token',
client_id: clientId,
btmpl: 'mobile',
Expand Down Expand Up @@ -334,6 +329,7 @@ OAuth.authorizeExplicitly = function(authURL, tokenURL, clientId, clientSecret,
webview,
spinner,
nav,
callbackUrl = this.callbackUrl,
retryCount = 0,
state = generateGUID(),
next = function(err, code) {
Expand All @@ -348,13 +344,13 @@ OAuth.authorizeExplicitly = function(authURL, tokenURL, clientId, clientSecret,
post(tokenURL, {
grant_type: 'authorization_code',
code: code,
redirect_uri: CALLBACK_URL,
redirect_uri: callbackUrl,
client_id: clientId,
client_secret: clientSecret
}, callback);
};

win = Ti.UI.createWindow(AUTH_WINDOW_OPTIONS);
win = Ti.UI.createWindow(this.authWindowOptions);

if (isiOS === true) {
var closeButton = Ti.UI.createButton({
Expand Down Expand Up @@ -387,7 +383,7 @@ OAuth.authorizeExplicitly = function(authURL, tokenURL, clientId, clientSecret,
url : buildURL(authURL, {
response_type: 'code',
client_id: clientId,
redirect_uri: CALLBACK_URL,
redirect_uri: this.callbackUrl,
scope: scopes,
approval_prompt: 'force',
btmpl: 'mobile',
Expand Down Expand Up @@ -424,7 +420,6 @@ OAuth.authorizeExplicitly = function(authURL, tokenURL, clientId, clientSecret,
}
};


/**
* Authorizes with client_id/client_secret for OAuth 2.0
*
Expand Down