diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..4143e75 --- /dev/null +++ b/.babelrc @@ -0,0 +1,4 @@ +{ + "presets": ["env"], + "plugins": ["transform-async-to-generator", "transform-object-rest-spread"] +} diff --git a/.gitignore b/.gitignore index 82a7772..0a45abc 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,7 @@ npm-debug.log project.xcworkspace/ xcuserdata/ +.idea +.vscode +javac-services.0.log* +dist/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..7e97da6 --- /dev/null +++ b/.npmignore @@ -0,0 +1,14 @@ +*.swp +*~ +*.iml +.*.haste_cache.* +.DS_Store +.idea +.babelrc +.eslintrc +npm-debug.log +src/ +examples/ +public/ +scripts/ +test/ \ No newline at end of file diff --git a/README.md b/README.md index 2c371a0..30427a1 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,20 @@ ## react-native-oauth -The `react-native-oauth` library provides an interface to OAuth 1.0 and OAuth 2.0 providers, such as [Twitter](http://twitter.com) and [Facebook](http://facebook.com) to React native. +The `react-native-oauth` library provides an interface to OAuth 1.0 and OAuth 2.0 providers with support for the following providers for React Native apps: + +* Twitter +* Facebook +* Google +* Github +* Slack ## TL;DR; This library cuts out the muck of dealing with the [OAuth 1.0](https://tools.ietf.org/html/rfc5849) and [OAuth 2.0](http://oauth.net/2/) protocols in react-native apps. The API is incredibly simple and straight-forward and is intended on getting you up and running quickly with OAuth providers (such as Facebook, Github, Twitter, etc). ```javascript +import OAuthManager from 'react-native-oauth'; + const manager = new OAuthManager('firestackexample') manager.configure({ twitter: { @@ -26,11 +34,18 @@ manager.authorize('google', {scopes: 'profile email'}) .catch(err => console.log('There was an error')); ``` +### Help + +Due to other time contraints, I cannot continue to work on react-native-oauth for the time it deserves. If you're interested in supporting this library, please help! It's a widely used library and I'd love to continue supporting it. Looking for maintainers! + ## Features * Isolates the OAuth experience to a few simple methods. * Atomatically stores the tokens for later retrieval -* Works with many providers and relatively simple to add a provider +* Works with many providers and simple to add new providers +* Works on both Android and iOS +* Makes calling API methods a snap +* Integrates seamlessly with Firestack (but can be used without it) ## Installation @@ -44,40 +59,52 @@ As we are integrating with react-native, we have a little more setup to integrat ### iOS setup -#### Automatically with [rnpm](https://github.com/rnpm/rnpm) +**Important**: This will _not_ work if you do not complete all the steps: -To automatically link our `react-native-oauth` client to our application, use the `rnpm` tool. [rnpm](https://github.com/rnpm/rnpm) is a React Native package manager which can help to automate the process of linking package environments. +- [ ] Link the `RCTLinkingManager` project +- [ ] Update your `AppDelegate.h` file +- [ ] Add KeychainSharing in your app +- [ ] Link the `react-native-oauth` project with your application (`react-native link`) +- [ ] Register a URL type of your application (Info tab -- see below) -```bash -rnpm link -``` +#### RCTLinkingManager + +Since `react-native-oauth` depends upon the `RCTLinkingManager` (from react-native core), we'll need to make sure we link this in our app. -#### Manually +In your app, add the following line to your `HEADER SEARCH PATHS`: + +``` +$(SRCROOT)/../node_modules/react-native-oauth/ios/OAuthManager +$(SRCROOT)/../node_modules/react-native/Libraries/LinkingIOS +``` -If you prefer not to use `rnpm`, we can manually link the package together with the following steps, after `npm install`: +![](./resources/header-search-paths.png) -1. In XCode, right click on `Libraries` and find the `Add Files to [project name]`. +Next, navigate to the neighboring "Build Phases" section of project settings, find the "Link Binary with Library" drop down, expand it, and click the "+" to add _libOAuthManager.a_ to the list. -![Add library to project](http://d.pr/i/2gEH.png) +Make sure to Update your `AppDelegate.m` as below, otherwise it will _not_ work. -2. Add the `node_modules/react-native-oauth/ios/OAuthManager.xcodeproj` +#### Automatically with [rnpm](https://github.com/rnpm/rnpm) -![OAuthManager.xcodeproj in Libraries listing](http://d.pr/i/19ktP.png) +To automatically link our `react-native-oauth` client to our application, use the `rnpm` tool. [rnpm](https://github.com/rnpm/rnpm) is a React Native package manager which can help to automate the process of linking package environments. -3. In the project's "Build Settings" tab in your app's target, add `libOAuthManager.a` to the list of `Link Binary with Libraries` +```bash +react-native link react-native-oauth +``` -![Linking binaries](http://d.pr/i/1cHgs.png) +Note: due to some restrictions on iOS, this module requires you to install cocoapods. The process has been semi-automated through using the above `react-native link` command. -4. Ensure that the `Build Settings` of the `OAuthManager.xcodeproj` project is ticked to _All_ and it's `Header Search Paths` include both of the following paths _and_ are set to _recursive_: +Once you have linked this library, run the following command in the root directory: - 1. `$(SRCROOT)/../../react-native/React` - 2. `$(SRCROOT)/../node_modules/react-native/React` +``` +(cd ios && pod install) +``` -![Recursive paths](http://d.pr/i/1hAr1.png) +Open in xcode the created `.xcworkspace` in the `ios/` directory (**NOT THE `.xproj` file**) when it's complete. -### Android setup +When working on iOS 10, we'll need to enable _Keychain Sharing Entitlement_ in _Capabilities_ of the build target of `io.fullstack.oauth.AUTH_MANAGER`. -Coming soon (looking for contributors). +![](./resources/capabilities.png) ## Handle deep linking loading @@ -90,14 +117,95 @@ We'll need to handle app loading from a url with our app in order to handle auth We need to add a callback method in our `ios/AppDelegate.m` file and then call our OAuthManager helper method. Let's load the `ios/AppDelegate.m` file and add the following all the way at the bottom (but before the `@end`): ```objectivec -- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url - sourceApplication:(NSString *)sourceApplication annotation:(id)annotation +// Add the import at the top: +#import "OAuthManager.h" +// ... +@implementation AppDelegate +// ... +- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { + return [OAuthManager handleOpenUrl:application + openURL:url + sourceApplication:sourceApplication + annotation:annotation]; +} +``` + +In addition, we'll need to set up the handlers within the iOS app. Add the following line somewhere in your `application:didFinishLaunchingWithOptions:` method, like so: + +```objectivec +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - return [OAuthManager handleOpenUrl:application openURL:url sourceApplication:sourceApplication annotation:annotation]; + NSURL *jsCodeLocation; + + jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; + + // other existing setup here + + // ADD THIS LINE SOMEWHERE IN THIS FUNCTION + [OAuthManager setupOAuthHandler:application]; + // ... + + [self.window makeKeyAndVisible]; + return YES; } ``` -When our app loads up with a request that is coming back from OAuthManager _and_ matches the pattern of `[app-name]://oauth-callback/{providerName}`, the OAuthManager will take over and handle the rest and storing the credentials for later use. +When our app loads up with a request that is coming back from OAuthManager _and_ matches the url pattern, OAuthManager will take over and handle the rest and storing the credentials for later use. + +### Adding URL schemes + +In order for our app to load through these callbacks, we need to tell our iOS app that we want to load them. In order to do that, we'll have to create some URL schemes to register our app. Some providers require specific schemes (mentioned later). + +These URL schemes can be added by navigating to to the `info` panel of our app in Xcode (see screenshot). + +![](./resources/info-panel.png) + +Let's add the appropriate one for our provider. For instance, to set up twitter, add the app name as a URL scheme in the URL scheme box. + +![](./resources/url-schemes.png) + +### Android setup + +After we link `react-native-oauth` to our application, we're ready to go. Android integration is much simpler, thanks to the in-app browser ability for our apps. `react-native-oauth` handles this for you. + +One note, *all* of the callback urls follow the scheme: `http://localhost/[provider_name]`. Make sure this is set as a configuration for each provider below (documented in the provider setup sections). + +Make sure you add the following to your `android/build.gradle` file: + +``` +maven { url "https://jitpack.io/" } +``` + +For instance, an example `android/build.gradle` file would look like this: + +``` +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + // ... +} + +allprojects { + repositories { + mavenLocal() + jcenter() + maven { url "https://jitpack.io/" } // <~ ADD THIS LINE + maven { + url "$rootDir/../node_modules/react-native/android" + } + } +} +``` + +## Creating the manager + +In our JS, we can create the manager by instantiating a new instance of it using the `new` method and passing it the name of our app: + +```javascript +const manager = new OAuthManager('firestackexample') +``` + +We need to pass the name of our app as the oauth manager uses this to create callback keys. This _must_ match the URL route created in your iOS app. For instance, above we created a URL scheme for Twitter. Pass this as the string in the `OAuthManager` constructor. ## Configuring our providers @@ -115,9 +223,16 @@ const config = { twitter: { consumer_key: 'SOME_CONSUMER_KEY', consumer_secret: 'SOME_CONSUMER_SECRET' + }, + facebook: { + client_id: 'YOUR_CLIENT_ID', + client_secret: 'YOUR_CLIENT_SECRET' } } -authManager.configureProvider("twitter", config.twitter); +// Create the manager +const manager = new OAuthManager('firestackexample') +// configure the manager +manager.configure(config); ``` The `consumer_key` and `consumer_secret` values are _generally_ provided by the provider development program. In the case of [twitter](https://apps.twitter.com), we can create an app and generate these values through their [development dashboard](https://apps.twitter.com). @@ -126,55 +241,289 @@ The `consumer_key` and `consumer_secret` values are _generally_ provided by the The following list are the providers we've implemented thus far in `react-native-oauth` and the _required_ keys to pass when configuring the provider: -* Twitter - * consumer_key - * consumer_secret -* Facebook (not fully implemented) - * consumer_key - * consumer_secret +#### Twitter (iOS/Android) + +To authenticate against twitter, we need to register a Twitter application. Register your twitter application (or create a new one at [apps.twitter.com](https://apps.twitter.com)). + +![](./resources/twitter/app.png) + +Once you have created one, navigate to the application and find the `Keys and Access Tokens`. Take note of the consumer key and secret: + +![](./resources/twitter/api-key.png) + +For the authentication to work properly, you need to set the Callback URL. It doesn't matter what you choose as long as its a valid url. + +![](./resources/twitter/callback-url.png) + +Twitter's URL scheme needs to be the app name (that we pass into the constructor method). Make sure we have one registered in Xcode as the same name: + +![](./resources/twitter/url-scheme.png) + +Add these values to the authorization configuration to pass to the `configure()` method as: + +```javascript +const config = { + twitter: { + consumer_key: 'SOME_CONSUMER_KEY', + consumer_secret: 'SOME_CONSUMER_SECRET' + } +} +``` + +#### Facebook (iOS/Android) + +To add facebook authentication, we'll need to have a Facebook app. To create one (or use an existing one), navigate to [developers.facebook.com/](https://developers.facebook.com/). + +![](./resources/facebook/dev.facebook.png) + +Find or create an application and find the app id. Take note of this app id. Next, navigate to the `Settings` panel and find your client_secret. + +![](./resources/facebook/app.png) + +Before we leave the Facebook settings, we need to tell Facebook we have a new redirect url to register. Navigate to the bottom of the page and add the following into the `bundle ID` field: + +`fb{YOUR_APP_ID}` + +For instance, my app ID in this example is: `1745641015707619`. In the `Bundle ID` field, I have added `fb1745641015707619`. + +![](./resources/facebook/redirect-url.png) + +For Android, you will also need to set the redirect url to `http://localhost/facebook` in the Facebook Login settings. + +![](./resources/facebook/redirect-url.png) + +We'll need to create a new URL scheme for Facebook and (this is a weird bug on the Facebook side) the facebook redirect URL scheme _must be the first one_ in the list. The URL scheme needs to be the same id as the `Bundle ID` copied from above: + +![](./resources/facebook/facebook-redirect.png) + +Back in our application, add the App ID and the secret as: + +```javascript +const config = { + facebook: { + client_id: 'YOUR_APP_ID', + client_secret: 'YOUR_APP_SECRET' + } +} +``` + +#### Google (iOS) + +To add Google auth to our application, first we'll need to create a google application. Create or use an existing one by heading to the [developers.google.com/](https://developers.google.com/) page (or the console directly at [https://console.developers.google.com](https://console.developers.google.com)). + +![](./resources/google/auth-page.png) + +We need to enable the `Identity Toolkit API` API. Click on `Enable API` and add this api to your app. Once it's enabled, we'll need to collect our credentials. + +Navigate to the `Credentials` tab and create a new credential. Create an **iOS API credential**. Take note of the `client_id` and the `iOS URL scheme`. In addition, make sure to set the bundle ID as the bundle id in our application in Xcode: + +![](./resources/google/creds.png) + +Take note of the `iOS URL Scheme`. We'll need to add this as a URL scheme in our app. In the `Info` panel of our app target (in Xcode), add the URL scheme: + +![](./resources/google/url-scheme.png) + +Finally, add the `client_id` credential as the id from the url page as well as the ios scheme (with any path) in our app configuration: + +```javascript +const config = { + google: { + callback_url: `[IOS SCHEME]:/google`, + client_id: 'YOUR_CLIENT_ID' + } +} +``` + +#### Google (Android) + +To set up Google on Android, follow the same steps as before, except this time instead of creating an iOS API, create a **web api credential**. Make sure to add the **redirect url** at the bottom (it defaults to `http://localhost/google`): + +![](./resources/google/android-creds.png) + +When creating an Android-specific configuration, create a file called `config/development.android.js`. React Native will load it instead of the `config/development.js` file automatically on Android. + +#### Github (iOS/Android) + +Adding Github auth to our application is pretty simple as well. We'll need to create a web application on the github apps page, which can be found at [https://github.com/settings/developers](https://github.com/settings/developers). Create one, making sure to add _two_ apps (one for iOS and one for Android) with the callback urls as: + +* ios: [app_name]:// oauth (for example: `firestackexample://oauth`) +* android: http://localhost/github + +Take note of the `client_id` and `client_secret` + +![](./resources/github/apps.png) + +The `iOS URL Scheme` is the same as the twitter version, which means we'll just add the app name as a URL scheme (i.e. `firestackexample`). + +Add the `client_id` and `client_secret` credentials to your configuration object: + +```javascript +const config = { + github: { + client_id: 'YOUR_CLIENT_ID', + client_secret: 'YOUR_CLIENT_SECRET' + } +} +``` + +## Slack + +We'll need to create an app first. Head to the slack developer docs at [https://slack.com/developers](https://slack.com/developers). + +![](./resources/slack/dev.png) + +Click on the Getting Started button: + +![](./resources/slack/getting_started.png) + + From here, find the `create an app` link: + +![](./resources/slack/create.png) + + Take note of the `client_id` and the `client_secret`. We'll place these in our configuration object just like so: + +```javascript +const config = { + slack: { + client_id: 'YOUR_CLIENT_ID', + client_secret: 'YOUR_CLIENT_SECRET' + } +} +``` + +Lastly, Slack requires us to add a redirect_url. + +For **iOS**: the callback_url pattern is `${app_name}://oauth`, so make sure to add your redirect_url where it asks for them before starting to work with the API. + +for **Android**: the `callback_url` pattern is `http://localhost/slack`. Be sure to add this to your list of redirect urls. + +![](./resources/slack/redirect.png) ## Authenticating against our providers -In order to make any authenticated calls against a provider, we need to authenticate against it. The `react-native-oauth` library passes through an easy method for dealing with authentication with the `authorizeWithCallbackURL()` method. +We can use the manager in our app using the `authorize()` method on the manager. + +The `authorize` method takes two arguments (the first one is required): -Using the app uri we previous setup, we can call the `authorizeWithCallbackURL()` method to ask iOS to redirect our user to a browser where they can log in to our app in the usual flow. When the user authorizes the login request, the promise returned by the `authorizeWithCallbackURL()` is resolved. If they reject the login request for any reason, the promise is rejected along with an error, if there are any. +* The provider we wish to authenticate against (i.e. twitter, facebook) +* The list of options on a per-provider basis (optional) + +For example: ```javascript -authManager.authorizeWithCallbackURL('twitter', 'firebase-example://oauth-callback/twitter') -.then((oauthResponse) => { - // the oauthResponse object is the response returned by the request - // which is later stored by react-native-oauth using AsyncStorage -}) -.catch((err) => { - // err is an error object that contains the reason the user - // error rejected authentication. -}) +manager.authorize('twitter') + .then(resp => console.log(resp)) + .catch(err => console.log(err)); +``` + +This method returns a promise that is resolved once the authentication has been completed. You'll get access to the authentication keys in the `resp` object. + +The `resp` object is set as follows: + +```javascript +{ + status: "ok", + response: { + authorized: true, (boolean) + uuid: "UUID", (user UUID) + credentials: { + access_token: "access token", + refresh_token: "refresh token", + type: 1 + } + } +} ``` -When the response is returned, `react-native-oauth` will store the resulting credentials using the `AsyncStorage` object provided natively by React Native. All of this happens behinds the scenes _automatically_. When the credentials are successfully rerequested, `AsyncStorage` is updated behind the scenes automatically. All you have to do is take care of authenticating the user using the `authorizeWithCallbackURL()` method. +The second argument accepts an object where we can ask for additional scopes, override default values, etc. + +```javascript +manager.authorize('google', {scopes: 'email,profile'}) + .then(resp => console.log(resp)) + .catch(err => console.log(err)); +``` + +* Scopes are a list of scopes _comma separated_ as a string. ## Calling a provider's API -Lastly, we can use our new oauth token to make requests to the api to make authenticated, signed requests. For instance, to get a list of the mentions on twitter, we can make a request at the endpoint: `'https://api.twitter.com/1.1/statuses/user_timeline.json'`. Provided our user has been authorized (or previously authorized), we can make a request using these credentials using the `makeRequest()` method. The `makeRequest()` method accepts between three and five parameters and returns a promise: +We can use OAuthManager to make requests to endpoints from our providers as well. For instance, let's say we want to get a user's time line from twitter. We would make the request to the url [https://api.twitter.com/1.1/statuses/user_timeline.json](https://api.twitter.com/1.1/statuses/user_timeline.json) + +If our user has been authorized for thi request, we can execute the request using the credentials stored by the OAuthManager. + +The `makeRequest()` method accepts 3 parameters: + +1. The provider we're making a request to +2. The url (or path) we want to make the request +3. Any additional options + +We can pass a list of options for our request with the last argument. The keys OAuthManager recognizes are: + +1. `params` - The query parameters +2. `method` - The http method to make the request with. + +Available HTTP methods: + * get + * post + * put + * delete + * head + * options + * trace -1. The provider our user is making a request (twitter, facebook, etc) -2. The HTTP method to use to make the request, for instance `get` or `post` -3. The URL to make the request -4. (optional) parameters to pass through directly to the request -5. (optional) headers are any headers associated with the request ```javascript const userTimelineUrl = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; -authManager.makeRequest('twitter', 'get', userTimelineUrl) +manager + .makeRequest('twitter', userTimelineUrl) + .then(resp => { + console.log('Data ->', resp.data); + }); +``` + +"me" represents the authenticated user, in any call to the Google+ API + +```javascript +const googleUrl = 'https://www.googleapis.com/plus/v1/people/me'; +manager + .makeRequest('google', googleUrl) + .then(resp => { + console.log('Data -> ', resp.data); + }); + +``` + +It's possible to use just the path as well. For instance, making a request with Facebook at the `/me` endpoint can be: + +```javascript +manager + .makeRequest('facebook', '/me') .then(resp => { - // resp is an object that includes both a `response` object containing - // details of the returned response as well as a `data` object which is - // a STRING of the returned data. OAuthManager makes zero assumptions of - // the data type when returned and instead passes through the string response + console.log('Data ->', resp.data); + }); +``` + +To add more data to our requests, we can pass a third argument: + +```javascript +manager + .makeRequest('facebook', '/me', { + headers: { 'Content-Type': 'application/java' }, + params: { email: 'me+rocks@ari.io' } }) - .catch(err => { - // err is an object that contains the error called when the promise - // is rejected + .then(resp => { + console.log('Data ->', resp.data); + }); +``` + +## Getting authorized accounts + +Since OAuthManager handles storing user accounts, we can query it to see which accounts have already been authorized or not using `savedAccounts()`: + +```javascript +manager.savedAccounts() + .then(resp => { + console.log('account list: ', resp.accounts); }) ``` @@ -185,7 +534,22 @@ We can `deauthorize()` our user's from using the provider by calling the `deauth 1. The `provider` we want to remove from our user credentials. ```javascript -authManager.deauthorize('twitter'); +manager.deauthorize('twitter'); +``` + +## Adding your own providers + +To add your own providers you can use the `addProvider()` method and fill in your provider details: + +```javascript +manager.addProvider({ + 'name_of_provider': { + auth_version: '2.0', + authorize_url: 'https://provider.dev/oauth', + access_token_url: 'https://provider.dev/oauth/token', + callback_url: ({app_name}) => `${app_name}://oauth`, + } +}); ``` ## Contributing @@ -201,9 +565,8 @@ ___ ## TODOS: -[] Handle rerequesting tokens (automatically?) -[] Simplify method of adding providers - [] Complete [facebook](https://developers.facebook.com/docs/facebook-login) support - [] Add [github](https://developer.github.com/v3/oauth/) support - [] Add [Google]() support -[] Add Android support +* [x] Simplify method of adding providers +* [x] Add github(https://developer.github.com/v3/oauth/) support +* [x] Add Google support +* [x] Add Facebook support +* [x] Add Android support diff --git a/android/android.iml b/android/android.iml new file mode 100644 index 0000000..0ee1481 --- /dev/null +++ b/android/android.iml @@ -0,0 +1,145 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..b9ff010 --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,50 @@ +buildscript { + repositories { + jcenter() + maven { url "https://jitpack.io/" } + } + dependencies { + classpath 'com.android.tools.build:gradle:2.1.3' + } +} +// END + +apply plugin: 'com.android.library' +apply plugin: 'maven' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.1" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + multiDexEnabled true + } + buildTypes { + release { + minifyEnabled false + } + } + + android.packagingOptions { + exclude 'META-INF/LICENSE' + } +} + +allprojects { + repositories { + jcenter() + maven { url "https://jitpack.io/" } + } +} +// END + +dependencies { + compile 'com.facebook.react:react-native:+' + compile 'com.github.scribejava:scribejava-apis:3.4.1' + compile 'com.github.delight-im:Android-AdvancedWebView:v3.0.0' + compile 'com.google.code.gson:gson:+' +} diff --git a/android/gradle/wrapper/gradle-wrapper.jar b/android/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..13372ae Binary files /dev/null and b/android/gradle/wrapper/gradle-wrapper.jar differ diff --git a/android/gradle/wrapper/gradle-wrapper.properties b/android/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..c17c955 --- /dev/null +++ b/android/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Fri Sep 02 21:23:30 PDT 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip diff --git a/android/gradlew b/android/gradlew new file mode 100644 index 0000000..9d82f78 --- /dev/null +++ b/android/gradlew @@ -0,0 +1,160 @@ +#!/usr/bin/env bash + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn ( ) { + echo "$*" +} + +die ( ) { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; +esac + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules +function splitJvmOpts() { + JVM_OPTS=("$@") +} +eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS +JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" + +exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" diff --git a/android/gradlew.bat b/android/gradlew.bat new file mode 100644 index 0000000..aec9973 --- /dev/null +++ b/android/gradlew.bat @@ -0,0 +1,90 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windowz variants + +if not "%OS%" == "Windows_NT" goto win9xME_args +if "%@eval[2+2]" == "4" goto 4NT_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* +goto execute + +:4NT_args +@rem Get arguments from the 4NT Shell from JP Software +set CMD_LINE_ARGS=%$ + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/android/local.properties b/android/local.properties new file mode 100644 index 0000000..ffb1ddd --- /dev/null +++ b/android/local.properties @@ -0,0 +1,11 @@ +## This file is automatically generated by Android Studio. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must *NOT* be checked into Version Control Systems, +# as it contains information specific to your local configuration. +# +# Location of the SDK. This is only used by Gradle. +# For customization when using a Version Control System, please read the +# header note. +#Tue Apr 11 11:36:49 IST 2017 +sdk.dir=/Users/divyanshunegi/Downloads/adt-bundle-mac-x86_64-20140321/sdk diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..84af192 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerConstants.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerConstants.java new file mode 100644 index 0000000..281a035 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerConstants.java @@ -0,0 +1,5 @@ +package io.fullstack.oauth; + +public interface OAuthManagerConstants { + String CREDENTIALS_STORE_PREF_FILE = "oauth_manager"; +} \ No newline at end of file diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerDialogFragment.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerDialogFragment.java new file mode 100644 index 0000000..12202bb --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerDialogFragment.java @@ -0,0 +1,340 @@ +package io.fullstack.oauth; + +import android.annotation.SuppressLint; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.Context; +import android.content.Intent; +import android.content.res.Resources; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.text.TextUtils; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.Display; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewGroup.LayoutParams; +import android.view.Window; +import android.view.WindowManager; +import android.webkit.WebView; +import android.webkit.WebViewClient; +import android.widget.ProgressBar; +import android.widget.RelativeLayout; + +import com.facebook.react.bridge.ReactContext; +import com.github.scribejava.core.model.OAuth1AccessToken; + +import java.lang.reflect.Method; +import java.util.Set; + +import im.delight.android.webview.AdvancedWebView; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class OAuthManagerDialogFragment extends DialogFragment implements AdvancedWebView.Listener { + + private static final int WEBVIEW_TAG = 100001; + private static final int WIDGET_TAG = 100002; + + private static final String TAG = "OauthFragment"; + private OAuthManagerFragmentController mController; + + private ReactContext mReactContext; + private AdvancedWebView mWebView; + private ProgressBar mProgressBar; + + public static final OAuthManagerDialogFragment newInstance( + final ReactContext reactContext, + OAuthManagerFragmentController controller + ) { + Bundle args = new Bundle(); + OAuthManagerDialogFragment frag = + new OAuthManagerDialogFragment(reactContext, controller); + return frag; + } + + public OAuthManagerDialogFragment( + final ReactContext reactContext, + OAuthManagerFragmentController controller + ) { + this.mController = controller; + this.mReactContext = reactContext; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Dialog dialog = super.onCreateDialog(savedInstanceState); + dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); + return dialog; + } + + @Override + public void onStart() { + super.onStart(); + Dialog dialog = getDialog(); + if (dialog != null) { + dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + final Context context = mReactContext; + LayoutParams rootViewLayoutParams = this.getFullscreenLayoutParams(context); + + RelativeLayout rootView = new RelativeLayout(context); + + mProgressBar = new ProgressBar(context); + RelativeLayout.LayoutParams progressParams = new RelativeLayout.LayoutParams(convertDpToPixel(50f,context),convertDpToPixel(50f,context)); + progressParams.addRule(RelativeLayout.CENTER_IN_PARENT); + mProgressBar.setLayoutParams(progressParams); + mProgressBar.setIndeterminate(true); + + getDialog().setCanceledOnTouchOutside(true); + rootView.setLayoutParams(rootViewLayoutParams); + + // mWebView = (AdvancedWebView) rootView.findViewById(R.id.webview); + Log.d(TAG, "Creating webview"); + mWebView = new AdvancedWebView(context); +// mWebView.setId(WEBVIEW_TAG); + mWebView.setListener(this, this); + mWebView.setVisibility(View.VISIBLE); + mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.getSettings().setDomStorageEnabled(true); + + + LayoutParams layoutParams = this.getFullscreenLayoutParams(context); + //new LayoutParams( + // LayoutParams.FILL_PARENT, + // DIALOG_HEIGHT + // ); + // mWebView.setLayoutParams(layoutParams); + + rootView.addView(mWebView, layoutParams); + rootView.addView(mProgressBar,progressParams); + + // LinearLayout pframe = new LinearLayout(context); + // pframe.setId(WIDGET_TAG); + // pframe.setOrientation(LinearLayout.VERTICAL); + // pframe.setVisibility(View.GONE); + // pframe.setGravity(Gravity.CENTER); + // pframe.setLayoutParams(layoutParams); + + // rootView.addView(pframe, layoutParams); + + this.setupWebView(mWebView); + mController.getRequestTokenUrlAndLoad(mWebView); + + Log.d(TAG, "Loading view..."); + return rootView; + } + + private LayoutParams getFullscreenLayoutParams(Context context) { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + // DisplayMetrics metrics = context.getResources().getDisplayMetrics(); + Display display = wm.getDefaultDisplay(); + int realWidth; + int realHeight; + + if (Build.VERSION.SDK_INT >= 17){ + //new pleasant way to get real metrics + DisplayMetrics realMetrics = new DisplayMetrics(); + display.getRealMetrics(realMetrics); + realWidth = realMetrics.widthPixels; + realHeight = realMetrics.heightPixels; + + } else if (Build.VERSION.SDK_INT >= 14) { + //reflection for this weird in-between time + try { + Method mGetRawH = Display.class.getMethod("getRawHeight"); + Method mGetRawW = Display.class.getMethod("getRawWidth"); + realWidth = (Integer) mGetRawW.invoke(display); + realHeight = (Integer) mGetRawH.invoke(display); + } catch (Exception e) { + //this may not be 100% accurate, but it's all we've got + realWidth = display.getWidth(); + realHeight = display.getHeight(); + Log.e("Display Info", "Couldn't use reflection to get the real display metrics."); + } + + } else { + //This should be close, as lower API devices should not have window navigation bars + realWidth = display.getWidth(); + realHeight = display.getHeight(); + } + + return new LayoutParams(realWidth, realHeight); + } + + + private void setupWebView(AdvancedWebView webView) { + webView.setWebViewClient(new WebViewClient() { + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + return interceptUrl(view, url, true); + } + + @Override + public void onPageFinished(WebView view, String url) { + super.onPageFinished(view, url); + mProgressBar.setVisibility(View.GONE); + } + + @Override + public void onReceivedError(WebView view, int code, String desc, String failingUrl) { + Log.i(TAG, "onReceivedError: " + failingUrl); + super.onReceivedError(view, code, desc, failingUrl); + onError(desc); + } + + private boolean interceptUrl(WebView view, String url, boolean loadUrl) { + Log.i(TAG, "interceptUrl called with url: " + url); + + // url would be http://localhost/twitter?denied=xxx when it's canceled + Pattern p = Pattern.compile("\\S*denied\\S*"); + Matcher m = p.matcher(url); + if(m.matches()){ + Log.i(TAG, "authentication is canceled"); + return false; + } + + if (isCallbackUri(url, mController.getCallbackUrl())) { + mController.getAccessToken(mWebView, url); + return true; + } + + if (loadUrl) { + view.loadUrl(url); + } + + return false; + } + }); + } + + public void setComplete(final OAuth1AccessToken accessToken) { + Log.d(TAG, "Completed: " + accessToken); + } + + +// @Override +// public void onDismiss(final DialogInterface dialog) { +// super.onDismiss(dialog); +// Log.d(TAG, "Dismissing dialog"); +// } + + + // @Override + // void onCancel(DialogInterface dialog) { + // Log.d(TAG, "onCancel called for dialog"); + // onError("Cancelled"); + // } + + @SuppressLint("NewApi") + @Override + public void onResume() { + super.onResume(); + mWebView.onResume(); + Log.d(TAG, "onResume called"); + } + + @SuppressLint("NewApi") + @Override + public void onPause() { + Log.d(TAG, "onPause called"); + mWebView.onPause(); + super.onPause(); + } + + @Override + public void onDestroy() { + mWebView.onDestroy(); + this.mController = null; + // ... + super.onDestroy(); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent intent) { + super.onActivityResult(requestCode, resultCode, intent); + mWebView.onActivityResult(requestCode, resultCode, intent); + + Log.d(TAG, "onActivityResult: " + requestCode); + // ... + } + + @Override + public void onPageStarted(String url, Bitmap favicon) { + Log.d(TAG, "onPageStarted " + url); + } + + @Override + public void onPageFinished(String url) { + Log.d(TAG, "onPageFinished: " + url); + // mController.onComplete(url); + } + + @Override + public void onPageError(int errorCode, String description, String failingUrl) { + Log.e(TAG, "onPageError: " + failingUrl); + mController.onError(errorCode, description, failingUrl); + } + + @Override + public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) { } + + @Override + public void onExternalPageRequest(String url) { + Log.d(TAG, "onExternalPageRequest: " + url); + } + + private void onError(String msg) { + Log.e(TAG, "Error: " + msg); + } + + static boolean isCallbackUri(String uri, String callbackUrl) { + Uri u = null; + Uri r = null; + try { + u = Uri.parse(uri); + r = Uri.parse(callbackUrl); + } catch (NullPointerException e) { + return false; + } + + if (u == null || r == null) return false; + + boolean rOpaque = r.isOpaque(); + boolean uOpaque = u.isOpaque(); + if (uOpaque != rOpaque) return false; + + if (rOpaque) return TextUtils.equals(uri, callbackUrl); + if (!TextUtils.equals(r.getScheme(), u.getScheme())) return false; + if (u.getPort() != r.getPort()) return false; + if (!TextUtils.isEmpty(r.getPath()) && !TextUtils.equals(r.getPath(), u.getPath())) return false; + + Set paramKeys = r.getQueryParameterNames(); + for (String key : paramKeys) { + if (!TextUtils.equals(r.getQueryParameter(key), u.getQueryParameter(key))) return false; + } + + String frag = r.getFragment(); + if (!TextUtils.isEmpty(frag) && !TextUtils.equals(frag, u.getFragment())) return false; + return true; + } + + public static int convertDpToPixel(float dp, Context context){ + Resources resources = context.getResources(); + DisplayMetrics metrics = resources.getDisplayMetrics(); + float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); + return (int)px; + } +} diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerFragmentController.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerFragmentController.java new file mode 100644 index 0000000..5281699 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerFragmentController.java @@ -0,0 +1,395 @@ +package io.fullstack.oauth; + +import android.app.Fragment; +import android.app.FragmentTransaction; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; + +import com.facebook.react.bridge.ReactContext; +import com.github.scribejava.core.exceptions.OAuthConnectionException; +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.oauth.OAuth10aService; +import com.github.scribejava.core.oauth.OAuth20Service; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import im.delight.android.webview.AdvancedWebView; + +// Credit where credit is due: +// Mostly taken from +// https://github.com/wuman/android-oauth-client/blob/6e01b81b7319a6954a1156e8b93c0b5cbeb61446/library/src/main/java/com/wuman/android/auth/DialogFragmentController.java + +public class OAuthManagerFragmentController { + private static final String TAG = "OAuthManager"; + + private final android.app.FragmentManager fragmentManager; + private final Handler uiHandler; + + private ReactContext context; + private String providerName; + private String authVersion; + private OAuth10aService oauth10aService; + private OAuth20Service oauth20Service; + private String callbackUrl; + private OAuth1RequestToken oauth1RequestToken; + private HashMap mCfg; + private AdvancedWebView mWebView; + + private Runnable onAccessToken; + private OAuthManagerOnAccessTokenListener mListener; + + private void runOnMainThread(Runnable runnable) { + uiHandler.post(runnable); + } + + public OAuthManagerFragmentController( + final ReactContext mReactContext, + android.app.FragmentManager fragmentManager, + final String providerName, + OAuth10aService oauthService, + final String callbackUrl + ) { + this.uiHandler = new Handler(Looper.getMainLooper()); + this.fragmentManager = fragmentManager; + + this.context = mReactContext; + this.providerName = providerName; + this.authVersion = "1.0"; + this.oauth10aService = oauthService; + this.callbackUrl = callbackUrl; + } + + public OAuthManagerFragmentController( + final ReactContext mReactContext, + android.app.FragmentManager fragmentManager, + final String providerName, + OAuth20Service oauthService, + final String callbackUrl + ) { + this.uiHandler = new Handler(Looper.getMainLooper()); + this.fragmentManager = fragmentManager; + + this.context = mReactContext; + this.providerName = providerName; + this.authVersion = "2.0"; + this.oauth20Service = oauthService; + this.callbackUrl = callbackUrl; + } + + + public void requestAuth(HashMap cfg, OAuthManagerOnAccessTokenListener listener) { + mListener = listener; + mCfg = cfg; + + runOnMainThread(new Runnable() { + @Override + public void run() { + Log.d(TAG, "fragment manager checking..."); + if (fragmentManager.isDestroyed()) { + return; + } + + FragmentTransaction ft = fragmentManager.beginTransaction(); + Fragment prevDialog = + fragmentManager.findFragmentByTag(TAG); + + Log.d(TAG, "previous() Dialog?"); + + if (prevDialog != null) { + ft.remove(prevDialog); + } + + Log.d(TAG, "Creating new Fragment"); + OAuthManagerDialogFragment frag = + OAuthManagerDialogFragment.newInstance(context, OAuthManagerFragmentController.this); + + ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); + ft.add(frag, TAG); + Log.d(TAG, "Committing with State Loss"); + // ft.commit(); + ft.commitAllowingStateLoss(); + } + }); + } + + private void dismissDialog() { + runOnMainThread(new Runnable() { + public void run() { + OAuthManagerDialogFragment frag = + (OAuthManagerDialogFragment) fragmentManager.findFragmentByTag(TAG); + + if (frag != null) { + frag.dismissAllowingStateLoss(); + } + } + }); + } + + public void setRequestToken( + final OAuth1RequestToken requestToken + ) { + this.oauth1RequestToken = requestToken; + } + + public void loaded10aAccessToken(final OAuth1AccessToken accessToken) { + Log.d(TAG, "Loaded access token in OAuthManagerFragmentController"); + Log.d(TAG, "AccessToken: " + accessToken + " (raw: " + accessToken.getRawResponse() + ")"); + + mWebView = null; + this.dismissDialog(); + mListener.onOAuth1AccessToken(accessToken); + } + + public void loaded20AccessToken(final OAuth2AccessToken accessToken) { + mWebView = null; + this.dismissDialog(); + mListener.onOAuth2AccessToken(accessToken); + } + + public void onComplete(String url) { + Log.d(TAG, "onComplete called in fragment controller " + url); + // if (mWebView != null) { + // this.getAccessToken(mWebView, url); + // } else { + // this.dismissDialog(); + // } + } + + public void onError(int errorCode, String description, String failingUrl) { + Log.e(TAG, "Error in OAuthManagerFragmentController: " + description); + this.dismissDialog(); + mListener.onRequestTokenError(new Exception(description)); + } + + public void getRequestTokenUrlAndLoad(AdvancedWebView webView) { + mWebView = webView; + LoadRequestTokenTask task = new LoadRequestTokenTask(this, webView); + task.execute(); + } + + public void getAccessToken( + final AdvancedWebView webView, + final String url + ) { + Uri responseUri = Uri.parse(url); + if (authVersion.equals("1.0")) { + String oauthToken = responseUri.getQueryParameter("oauth_token"); + String oauthVerifier = responseUri.getQueryParameter("oauth_verifier"); + Load1AccessTokenTask task = new Load1AccessTokenTask( + this, webView, oauth1RequestToken, oauthVerifier); + task.execute(); + } else if (authVersion.equals("2.0")) { + String code = responseUri.getQueryParameter("code"); + Log.d(TAG, "Called getAccessToken with code: " + code + " at " + url); + if (code != null) { + Load2AccessTokenTask task = new Load2AccessTokenTask( + this, webView, code); + task.execute(); + } else { + this.dismissDialog(); + mListener.onRequestTokenError(new Exception("No token found")); + } + } + } + + ////// TASKS + + private abstract class OAuthTokenTask + extends AsyncTask { + protected AdvancedWebView mWebView; + protected OAuthManagerFragmentController mCtrl; + + public OAuthTokenTask( + OAuthManagerFragmentController ctrl, + AdvancedWebView webView + ) { + this.mCtrl = ctrl; + this.mWebView = webView; + } + + @Override + protected Result doInBackground(Void... params) { + return null; + } + + @Override + protected void onPostExecute(final Result result) {} + } + + private class LoadRequestTokenTask extends OAuthTokenTask { + private OAuth1RequestToken oauth1RequestToken; + + public LoadRequestTokenTask( + OAuthManagerFragmentController ctrl, + AdvancedWebView view + ) { + super(ctrl, view); + } + + @Override + protected String doInBackground(Void... params) { + try { + if (authVersion.equals("1.0")) { + oauth1RequestToken = oauth10aService.getRequestToken(); + + final String requestTokenUrl = + oauth10aService.getAuthorizationUrl(oauth1RequestToken); + return requestTokenUrl; + } else if (authVersion.equals("2.0")) { + + String authorizationUrl; + + if (mCfg.containsKey("authorization_url_params")) { + final HashMap additionalParams = new HashMap(); + additionalParams.put("access_type", "offline"); + additionalParams.put("prompt", "consent"); + + Map authUrlMap = (Map) mCfg.get("authorization_url_params"); + if (authUrlMap != null) { + if (authUrlMap.containsKey("access_type")) { + additionalParams.put("access_type", (String) authUrlMap.get("access_type")); + } + if (authUrlMap.containsKey("prompt")) { + additionalParams.put("prompt", (String) authUrlMap.get("prompt")); + } + } + authorizationUrl = oauth20Service.getAuthorizationUrl(additionalParams); + } else { + authorizationUrl = oauth20Service.getAuthorizationUrl(); + } + + return authorizationUrl; + } else { + return null; + } + } catch (OAuthConnectionException ex) { + Log.e(TAG, "OAuth connection exception: " + ex.getMessage()); + ex.printStackTrace(); + return null; + } catch (IOException ex) { + Log.e(TAG, "IOException occurred: "+ ex.getMessage()); + ex.printStackTrace(); + return null; + } + } + + @Override + protected void onPostExecute(final String url) { + runOnMainThread(new Runnable() { + @Override + public void run() { + if (url == null) { + mCtrl.onError(-1, "No url", ""); + return; + } + if (authVersion.equals("1.0")) { + mCtrl.setRequestToken(oauth1RequestToken); + mWebView.loadUrl(url); + } else if (authVersion.equals("2.0")) { + mWebView.loadUrl(url); + } + } + }); + } + } + + private class Load1AccessTokenTask extends OAuthTokenTask { + private String oauthVerifier; + + public Load1AccessTokenTask( + OAuthManagerFragmentController ctrl, + AdvancedWebView view, + OAuth1RequestToken requestToken, + String oauthVerifier + ) { + super(ctrl, view); + this.oauthVerifier = oauthVerifier; + } + + @Override + protected OAuth1AccessToken doInBackground(Void... params) { + try { + final OAuth1AccessToken accessToken = + (OAuth1AccessToken) oauth10aService.getAccessToken(oauth1RequestToken, oauthVerifier); + return accessToken; + } catch (OAuthConnectionException ex) { + Log.e(TAG, "OAuth connection exception: " + ex.getMessage()); + ex.printStackTrace(); + return null; + } catch (IOException ex) { + Log.e(TAG, "An exception occurred getRequestToken: " + ex.getMessage()); + ex.printStackTrace(); + return null; + } + } + + @Override + protected void onPostExecute(final OAuth1AccessToken accessToken) { + runOnMainThread(new Runnable() { + @Override + public void run() { + if (accessToken == null) { + mCtrl.onError(-1, "No accessToken found", ""); + return; + } + mCtrl.loaded10aAccessToken(accessToken); + } + }); + } + } + + private class Load2AccessTokenTask extends OAuthTokenTask { + private String authorizationCode; + + public Load2AccessTokenTask( + OAuthManagerFragmentController ctrl, + AdvancedWebView view, + String authorizationCode + ) { + super(ctrl, view); + this.authorizationCode = authorizationCode; + } + + @Override + protected OAuth2AccessToken doInBackground(Void... params) { + try { + final OAuth2AccessToken accessToken = + (OAuth2AccessToken) oauth20Service.getAccessToken(authorizationCode); + return accessToken; + } catch (OAuthConnectionException ex) { + Log.e(TAG, "OAuth connection exception: " + ex.getMessage()); + ex.printStackTrace(); + return null; + } catch (IOException ex) { + Log.e(TAG, "An exception occurred getRequestToken: " + ex.getMessage()); + ex.printStackTrace(); + return null; + } + } + + @Override + protected void onPostExecute(final OAuth2AccessToken accessToken) { + runOnMainThread(new Runnable() { + @Override + public void run() { + if (accessToken == null) { + mCtrl.onError(-1, "No accessToken found", ""); + return; + } + mCtrl.loaded20AccessToken(accessToken); + } + }); + } + } + + public String getCallbackUrl() { + return this.callbackUrl; + } +} diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java new file mode 100644 index 0000000..4ac136f --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java @@ -0,0 +1,577 @@ +package io.fullstack.oauth; + +import android.app.Activity; +import android.app.FragmentManager; +import android.content.Context; +import android.support.annotation.Nullable; +import android.util.Log; + +import com.google.gson.Gson; +import com.google.gson.JsonSyntaxException; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Callback; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.ReadableMapKeySetIterator; +import com.facebook.react.bridge.ReadableType; +import com.facebook.react.bridge.WritableMap; +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.model.OAuthRequest; +import com.github.scribejava.core.model.Response; +import com.github.scribejava.core.model.Verb; +import com.github.scribejava.core.oauth.OAuth10aService; +import com.github.scribejava.core.oauth.OAuth20Service; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class ProviderNotConfiguredException extends Exception { + public ProviderNotConfiguredException(String message) { + super(message); + } +} + +@SuppressWarnings("WeakerAccess") +class OAuthManagerModule extends ReactContextBaseJavaModule { + private static final String TAG = "OAuthManager"; + + private Context context; + private ReactContext mReactContext; + + private HashMap _configuration = new HashMap>(); + private ArrayList _callbackUrls = new ArrayList(); + private OAuthManagerStore _credentialsStore; + + public OAuthManagerModule(ReactApplicationContext reactContext) { + super(reactContext); + mReactContext = reactContext; + _credentialsStore = OAuthManagerStore.getOAuthManagerStore(mReactContext, TAG, Context.MODE_PRIVATE); + Log.d(TAG, "New instance"); + } + + @Override + public String getName() { + return TAG; + } + + @ReactMethod + public void configureProvider( + final String providerName, + final ReadableMap params, + @Nullable final Callback onComplete + ) { + Log.i(TAG, "configureProvider for " + providerName); + + // Save callback url for later + String callbackUrlStr = params.getString("callback_url"); + _callbackUrls.add(callbackUrlStr); + + Log.d(TAG, "Added callback url " + callbackUrlStr + " for providler " + providerName); + + // Keep configuration map + HashMap cfg = new HashMap(); + + ReadableMapKeySetIterator iterator = params.keySetIterator(); + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + ReadableType readableType = params.getType(key); + switch(readableType) { + case String: + String val = params.getString(key); + // String escapedVal = Uri.encode(val); + cfg.put(key, val); + break; + default: + throw new IllegalArgumentException("Could not read object with key: " + key); + } + } + + _configuration.put(providerName, cfg); + + onComplete.invoke(null, true); + } + + @ReactMethod + public void authorize( + final String providerName, + @Nullable final ReadableMap params, + final Callback callback) + { + try { + final OAuthManagerModule self = this; + final HashMap cfg = this.getConfiguration(providerName); + final String authVersion = (String) cfg.get("auth_version"); + Activity activity = this.getCurrentActivity(); + FragmentManager fragmentManager = activity.getFragmentManager(); + String callbackUrl = "http://localhost/" + providerName; + + OAuthManagerOnAccessTokenListener listener = new OAuthManagerOnAccessTokenListener() { + public void onRequestTokenError(final Exception ex) { + Log.e(TAG, "Exception with request token: " + ex.getMessage()); + _credentialsStore.delete(providerName); + _credentialsStore.commit(); + } + public void onOAuth1AccessToken(final OAuth1AccessToken accessToken) { + _credentialsStore.store(providerName, accessToken); + _credentialsStore.commit(); + + WritableMap resp = self.accessTokenResponse(providerName, cfg, accessToken, authVersion); + callback.invoke(null, resp); + } + public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) { + _credentialsStore.store(providerName, accessToken); + _credentialsStore.commit(); + + WritableMap resp = self.accessTokenResponse(providerName, cfg, accessToken, authVersion); + callback.invoke(null, resp); + } + }; + + if (authVersion.equals("1.0")) { + final OAuth10aService service = + OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, params, callbackUrl); + + OAuthManagerFragmentController ctrl = + new OAuthManagerFragmentController(mReactContext, fragmentManager, providerName, service, callbackUrl); + + ctrl.requestAuth(cfg, listener); + } else if (authVersion.equals("2.0")) { + final OAuth20Service service = + OAuthManagerProviders.getApiFor20Provider(providerName, cfg, params, callbackUrl); + + OAuthManagerFragmentController ctrl = + new OAuthManagerFragmentController(mReactContext, fragmentManager, providerName, service, callbackUrl); + + ctrl.requestAuth(cfg, listener); + } else { + Log.d(TAG, "Auth version unknown: " + (String) cfg.get("auth_version")); + } + } catch (Exception ex) { + Log.d(TAG, "Exception in callback " + ex.getMessage()); + exceptionCallback(ex, callback); + } + } + + @ReactMethod + public void makeRequest( + final String providerName, + final String urlString, + final ReadableMap params, + final Callback onComplete) { + + Log.i(TAG, "makeRequest called for " + providerName + " to " + urlString); + try { + HashMap cfg = this.getConfiguration(providerName); + final String authVersion = (String) cfg.get("auth_version"); + + URL url; + try { + if (urlString.contains("http")) { + url = new URL(urlString); + } else { + String apiHost = (String) cfg.get("api_url"); + url = new URL(apiHost + urlString); + } + } catch (MalformedURLException ex) { + Log.e(TAG, "Bad url. Check request and try again: " + ex.getMessage()); + exceptionCallback(ex, onComplete); + return; + } + + String httpMethod; + if (params.hasKey("method")) { + httpMethod = params.getString("method"); + } else { + httpMethod = "GET"; + } + + Verb httpVerb; + if (httpMethod.equalsIgnoreCase("GET")) { + httpVerb = Verb.GET; + } else if (httpMethod.equalsIgnoreCase("POST")) { + httpVerb = Verb.POST; + } else if (httpMethod.equalsIgnoreCase("PUT")) { + httpVerb = Verb.PUT; + } else if (httpMethod.equalsIgnoreCase("DELETE")) { + httpVerb = Verb.DELETE; + } else if (httpMethod.equalsIgnoreCase("OPTIONS")) { + httpVerb = Verb.OPTIONS; + } else if (httpMethod.equalsIgnoreCase("HEAD")) { + httpVerb = Verb.HEAD; + } else if (httpMethod.equalsIgnoreCase("PATCH")) { + httpVerb = Verb.PATCH; + } else if (httpMethod.equalsIgnoreCase("TRACE")) { + httpVerb = Verb.TRACE; + } else { + httpVerb = Verb.GET; + } + + ReadableMap requestParams = null; + if (params != null && params.hasKey("params")) { + requestParams = params.getMap("params"); + } + OAuthRequest request = oauthRequestWithParams(providerName, cfg, authVersion, httpVerb, url, requestParams); + + if (authVersion.equals("1.0")) { + final OAuth10aService service = + OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, requestParams, null); + OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class); + + service.signRequest(token, request); + } else if (authVersion.equals("2.0")) { + final OAuth20Service service = + OAuthManagerProviders.getApiFor20Provider(providerName, cfg, requestParams, null); + OAuth2AccessToken token = _credentialsStore.get(providerName, OAuth2AccessToken.class); + + service.signRequest(token, request); + } else { + // Some kind of error here + Log.e(TAG, "An error occurred"); + WritableMap err = Arguments.createMap(); + err.putString("status", "error"); + err.putString("msg", "A weird error occurred"); + onComplete.invoke(err); + return; + } + + final Response response = request.send(); + final String rawBody = response.getBody(); + + Log.d(TAG, "rawBody: " + rawBody); + // final Object response = new Gson().fromJson(rawBody, Object.class); + + WritableMap resp = Arguments.createMap(); + resp.putInt("status", response.getCode()); + resp.putString("data", rawBody); + onComplete.invoke(null, resp); + + } catch (IOException ex) { + Log.e(TAG, "IOException when making request: " + ex.getMessage()); + ex.printStackTrace(); + exceptionCallback(ex, onComplete); + } catch (Exception ex) { + Log.e(TAG, "Exception when making request: " + ex.getMessage()); + exceptionCallback(ex, onComplete); + } + } + + private OAuthRequest oauthRequestWithParams( + final String providerName, + final HashMap cfg, + final String authVersion, + final Verb httpVerb, + final URL url, + @Nullable final ReadableMap params + ) throws Exception { + OAuthRequest request; + // OAuthConfig config; + + if (authVersion.equals("1.0")) { + // final OAuth10aService service = + // OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null); + OAuth1AccessToken oa1token = _credentialsStore.get(providerName, OAuth1AccessToken.class); + request = OAuthManagerProviders.getRequestForProvider( + providerName, + httpVerb, + oa1token, + url, + cfg, + params); + + // config = service.getConfig(); + // request = new OAuthRequest(httpVerb, url.toString(), config); + } else if (authVersion.equals("2.0")) { + // final OAuth20Service service = + // OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null, null); + // oa2token = _credentialsStore.get(providerName, OAuth2AccessToken.class); + + OAuth2AccessToken oa2token = _credentialsStore.get(providerName, OAuth2AccessToken.class); + request = OAuthManagerProviders.getRequestForProvider( + providerName, + httpVerb, + oa2token, + url, + cfg, + params); + + // config = service.getConfig(); + // request = new OAuthRequest(httpVerb, url.toString(), config); + } else { + Log.e(TAG, "Error in making request method"); + throw new Exception("Provider not handled yet"); + } + + return request; + } + + @ReactMethod + public void getSavedAccounts(final ReadableMap options, final Callback onComplete) { + // Log.d(TAG, "getSavedAccounts"); + } + + @ReactMethod + public void getSavedAccount( + final String providerName, + final ReadableMap options, + final Callback onComplete) + { + try { + HashMap cfg = this.getConfiguration(providerName); + final String authVersion = (String) cfg.get("auth_version"); + + Log.i(TAG, "getSavedAccount for " + providerName); + + if (authVersion.equals("1.0")) { + OAuth1AccessToken token = _credentialsStore.get(providerName, OAuth1AccessToken.class); + Log.d(TAG, "Found token: " + token); + if (token == null || token.equals("")) { + throw new Exception("No token found"); + } + + WritableMap resp = this.accessTokenResponse(providerName, cfg, token, authVersion); + onComplete.invoke(null, resp); + } else if (authVersion.equals("2.0")) { + OAuth2AccessToken token = _credentialsStore.get(providerName, OAuth2AccessToken.class); + + if (token == null || token.equals("")) { + throw new Exception("No token found"); + } + WritableMap resp = this.accessTokenResponse(providerName, cfg, token, authVersion); + onComplete.invoke(null, resp); + } else { + + } + } catch (ProviderNotConfiguredException ex) { + Log.e(TAG, "Provider not yet configured: " + providerName); + exceptionCallback(ex, onComplete); + } catch (Exception ex) { + Log.e(TAG, "An exception occurred getSavedAccount: " + ex.getMessage()); + ex.printStackTrace(); + exceptionCallback(ex, onComplete); + } + + } + + @ReactMethod + public void deauthorize(final String providerName, final Callback onComplete) { + try { + Log.i(TAG, "deauthorizing " + providerName); + HashMap cfg = this.getConfiguration(providerName); + final String authVersion = (String) cfg.get("auth_version"); + + _credentialsStore.delete(providerName); + + WritableMap resp = Arguments.createMap(); + resp.putString("status", "ok"); + + onComplete.invoke(null, resp); + } catch (Exception ex) { + exceptionCallback(ex, onComplete); + } + } + + + private HashMap getConfiguration( + final String providerName + ) throws Exception { + if (!_configuration.containsKey(providerName)) { + throw new ProviderNotConfiguredException("Provider not configured: " + providerName); + } + + HashMap cfg = (HashMap) _configuration.get(providerName); + return cfg; + } + + private WritableMap accessTokenResponse( + final String providerName, + final HashMap cfg, + final OAuth1AccessToken accessToken, + final String oauthVersion + ) { + WritableMap resp = Arguments.createMap(); + WritableMap response = Arguments.createMap(); + + Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse()); + + /* Some things return as JSON, some as x-www-form-urlencoded (querystring) */ + + Map accessTokenMap = null; + try { + accessTokenMap = new Gson().fromJson(accessToken.getRawResponse(), Map.class); + } catch (JsonSyntaxException e) { + /* + failed to parse as JSON, so turn it into a HashMap which looks like the one we'd + get back from the JSON parser, so the rest of the code continues unchanged. + */ + Log.d(TAG, "Credential looks like a querystring; parsing as such"); + accessTokenMap = new HashMap(); + accessTokenMap.put("user_id", accessToken.getParameter("user_id")); + accessTokenMap.put("oauth_token_secret", accessToken.getParameter("oauth_token_secret")); + accessTokenMap.put("token_type", accessToken.getParameter("token_type")); + } + + + resp.putString("status", "ok"); + resp.putBoolean("authorized", true); + resp.putString("provider", providerName); + + String uuid = accessToken.getParameter("user_id"); + response.putString("uuid", uuid); + String oauthTokenSecret = (String) accessToken.getParameter("oauth_token_secret"); + + String tokenType = (String) accessToken.getParameter("token_type"); + if (tokenType == null) { + tokenType = "Bearer"; + } + + String consumerKey = (String) cfg.get("consumer_key"); + + WritableMap credentials = Arguments.createMap(); + credentials.putString("access_token", accessToken.getToken()); + credentials.putString("access_token_secret", oauthTokenSecret); + credentials.putString("type", tokenType); + credentials.putString("consumerKey", consumerKey); + + response.putMap("credentials", credentials); + + resp.putMap("response", response); + + return resp; + } + + private WritableMap accessTokenResponse( + final String providerName, + final HashMap cfg, + final OAuth2AccessToken accessToken, + final String oauthVersion + ) { + WritableMap resp = Arguments.createMap(); + WritableMap response = Arguments.createMap(); + + resp.putString("status", "ok"); + resp.putBoolean("authorized", true); + resp.putString("provider", providerName); + + String uuid = accessToken.getParameter("user_id"); + response.putString("uuid", uuid); + + WritableMap credentials = Arguments.createMap(); + Log.d(TAG, "Credential raw response: " + accessToken.getRawResponse()); + + credentials.putString("accessToken", accessToken.getAccessToken()); + String authHeader; + + String tokenType = accessToken.getTokenType(); + if (tokenType == null) { + tokenType = "Bearer"; + } + + String scope = accessToken.getScope(); + if (scope == null) { + scope = (String) cfg.get("scopes"); + } + + String clientID = (String) cfg.get("client_id"); + String idToken = accessToken.getParameter("id_token"); + + authHeader = tokenType + " " + accessToken.getAccessToken(); + credentials.putString("authorizationHeader", authHeader); + credentials.putString("type", tokenType); + credentials.putString("scopes", scope); + credentials.putString("clientID", clientID); + credentials.putString("idToken", idToken); + response.putMap("credentials", credentials); + + resp.putMap("response", response); + + return resp; + } + + + private void exceptionCallback(Exception ex, final Callback onFail) { + WritableMap error = Arguments.createMap(); + error.putInt("errorCode", ex.hashCode()); + error.putString("errorMessage", ex.getMessage()); + error.putString("allErrorMessage", ex.toString()); + + onFail.invoke(error); + } + + public static Map recursivelyDeconstructReadableMap(ReadableMap readableMap) { + Map deconstructedMap = new HashMap<>(); + if (readableMap == null) { + return deconstructedMap; + } + + ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + ReadableType type = readableMap.getType(key); + switch (type) { + case Null: + deconstructedMap.put(key, null); + break; + case Boolean: + deconstructedMap.put(key, readableMap.getBoolean(key)); + break; + case Number: + deconstructedMap.put(key, readableMap.getDouble(key)); + break; + case String: + deconstructedMap.put(key, readableMap.getString(key)); + break; + case Map: + deconstructedMap.put(key, OAuthManagerModule.recursivelyDeconstructReadableMap(readableMap.getMap(key))); + break; + case Array: + deconstructedMap.put(key, OAuthManagerModule.recursivelyDeconstructReadableArray(readableMap.getArray(key))); + break; + default: + throw new IllegalArgumentException("Could not convert object with key: " + key + "."); + } + + } + return deconstructedMap; + } + + public static List recursivelyDeconstructReadableArray(ReadableArray readableArray) { + List deconstructedList = new ArrayList<>(readableArray.size()); + for (int i = 0; i < readableArray.size(); i++) { + ReadableType indexType = readableArray.getType(i); + switch (indexType) { + case Null: + deconstructedList.add(i, null); + break; + case Boolean: + deconstructedList.add(i, readableArray.getBoolean(i)); + break; + case Number: + deconstructedList.add(i, readableArray.getDouble(i)); + break; + case String: + deconstructedList.add(i, readableArray.getString(i)); + break; + case Map: + deconstructedList.add(i, OAuthManagerModule.recursivelyDeconstructReadableMap(readableArray.getMap(i))); + break; + case Array: + deconstructedList.add(i, OAuthManagerModule.recursivelyDeconstructReadableArray(readableArray.getArray(i))); + break; + default: + throw new IllegalArgumentException("Could not convert object at index " + i + "."); + } + } + return deconstructedList; + } +} diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerOnAccessTokenListener.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerOnAccessTokenListener.java new file mode 100644 index 0000000..aed5103 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerOnAccessTokenListener.java @@ -0,0 +1,11 @@ +package io.fullstack.oauth; + +import java.io.IOException; +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuth2AccessToken; + +public interface OAuthManagerOnAccessTokenListener { + void onOAuth1AccessToken(final OAuth1AccessToken accessToken); + void onOAuth2AccessToken(final OAuth2AccessToken accessToken); + void onRequestTokenError(final Exception ex); +} \ No newline at end of file diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerPackage.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerPackage.java new file mode 100644 index 0000000..14c79e6 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerPackage.java @@ -0,0 +1,50 @@ +package io.fullstack.oauth; + +import android.content.Context; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.List; +import java.util.ArrayList; +import java.util.Collections; + +@SuppressWarnings("unused") +public class OAuthManagerPackage implements ReactPackage { + private Context mContext; + + public OAuthManagerPackage() { + } + /** + * @param reactContext react application context that can be used to create modules + * @return list of native modules to register with the newly created catalyst instance + */ + public List createNativeModules(ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + modules.add(new OAuthManagerModule(reactContext)); + return modules; + } + + /** + * @return list of JS modules to register with the newly created catalyst instance. + *

+ * IMPORTANT: Note that only modules that needs to be accessible from the native code should be + * listed here. Also listing a native module here doesn't imply that the JS implementation of it + * will be automatically included in the JS bundle. + */ + + public List> createJSModules() { + return Collections.emptyList(); + } + + /** + * @param reactContext + * @return a list of view managers that should be registered with {@link UIManagerModule} + */ + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } +} diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java new file mode 100644 index 0000000..86c64c3 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerProviders.java @@ -0,0 +1,295 @@ +package io.fullstack.oauth; + +import android.util.Log; +import java.util.HashMap; +import java.util.Random; +import java.util.List; +import android.support.annotation.Nullable; +import java.net.URL; +import java.net.MalformedURLException; +import android.text.TextUtils; +import java.util.Arrays; + +import com.github.scribejava.core.model.Verb; +import com.github.scribejava.core.builder.api.BaseApi; +import com.github.scribejava.core.oauth.OAuthService; +import com.github.scribejava.core.oauth.OAuth10aService; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuthRequest; +import com.github.scribejava.core.model.OAuthConfig; + +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.oauth.OAuth20Service; + +import com.github.scribejava.apis.TwitterApi; +import com.github.scribejava.apis.FacebookApi; +import com.github.scribejava.apis.GoogleApi20; +import com.github.scribejava.apis.GitHubApi; + +import com.github.scribejava.apis.ConfigurableApi; +import com.github.scribejava.apis.SlackApi; + +import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.ReadableArray; +import com.facebook.react.bridge.ReadableType; +import com.facebook.react.bridge.ReadableMapKeySetIterator; + +public class OAuthManagerProviders { + private static final String TAG = "OAuthManagerProviders"; + + static public OAuth10aService getApiFor10aProvider( + final String providerName, + final HashMap params, + @Nullable final ReadableMap opts, + final String callbackUrl + ) { + if (providerName.equalsIgnoreCase("twitter")) { + return OAuthManagerProviders.twitterService(params, opts, callbackUrl); + } else { + return null; + } + } + + static public OAuth20Service getApiFor20Provider( + final String providerName, + final HashMap params, + @Nullable final ReadableMap opts, + final String callbackUrl + ) { + if (providerName.equalsIgnoreCase("facebook")) { + return OAuthManagerProviders.facebookService(params, opts, callbackUrl); + } + + if (providerName.equalsIgnoreCase("google")) { + return OAuthManagerProviders.googleService(params, opts, callbackUrl); + } + + if (providerName.equalsIgnoreCase("github")) { + return OAuthManagerProviders.githubService(params, opts, callbackUrl); + } + + if (providerName.equalsIgnoreCase("slack")) { + return OAuthManagerProviders.slackService(params, opts, callbackUrl); + } + + if (params.containsKey("access_token_url") && params.containsKey("authorize_url")) { + return OAuthManagerProviders.configurableService(params, opts, callbackUrl); + } + + return null; + } + + static public OAuthRequest getRequestForProvider( + final String providerName, + final Verb httpVerb, + final OAuth1AccessToken oa1token, + final URL url, + final HashMap cfg, + @Nullable final ReadableMap params + ) { + final OAuth10aService service = + OAuthManagerProviders.getApiFor10aProvider(providerName, cfg, null, null); + + String token = oa1token.getToken(); + OAuthConfig config = service.getConfig(); + OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config); + + request = OAuthManagerProviders.addParametersToRequest(request, token, params); + // Nothing special for Twitter + return request; + } + + static public OAuthRequest getRequestForProvider( + final String providerName, + final Verb httpVerb, + final OAuth2AccessToken oa2token, + final URL url, + final HashMap cfg, + @Nullable final ReadableMap params + ) { + final OAuth20Service service = + OAuthManagerProviders.getApiFor20Provider(providerName, cfg, null, null); + + OAuthConfig config = service.getConfig(); + OAuthRequest request = new OAuthRequest(httpVerb, url.toString(), config); + String token = oa2token.getAccessToken(); + + request = OAuthManagerProviders.addParametersToRequest(request, token, params); + + // + Log.d(TAG, "Making request for " + providerName + " to add token " + token); + // Need a way to standardize this, but for now + if (providerName.equalsIgnoreCase("slack")) { + request.addParameter("token", token); + } + + return request; + } + + // Helper to add parameters to the request + static private OAuthRequest addParametersToRequest( + OAuthRequest request, + final String access_token, + @Nullable final ReadableMap params + ) { + if (params != null && params.hasKey("params")) { + ReadableMapKeySetIterator iterator = params.keySetIterator(); + while (iterator.hasNextKey()) { + String key = iterator.nextKey(); + ReadableType readableType = params.getType(key); + switch(readableType) { + case String: + String val = params.getString(key); + // String escapedVal = Uri.encode(val); + if (val.equals("access_token")) { + val = access_token; + } + request.addParameter(key, val); + break; + default: + throw new IllegalArgumentException("Could not read object with key: " + key); + } + } + } + return request; + } + + private static OAuth10aService twitterService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl) { + String consumerKey = (String) cfg.get("consumer_key"); + String consumerSecret = (String) cfg.get("consumer_secret"); + + ServiceBuilder builder = new ServiceBuilder() + .apiKey(consumerKey) + .apiSecret(consumerSecret) + .debug(); + + String scopes = (String) cfg.get("scopes"); + if (scopes != null) { + // String scopeStr = OAuthManagerProviders.getScopeString(scopes, "+"); + // Log.d(TAG, "scopeStr: " + scopeStr); + // builder.scope(scopeStr); + } + + if (callbackUrl != null) { + builder.callback(callbackUrl); + } + + return builder.build(TwitterApi.instance()); + } + + private static OAuth20Service facebookService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl) { + ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl); + return builder.build(FacebookApi.instance()); + } + + private static OAuth20Service googleService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl) + { + ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl); + return builder.build(GoogleApi20.instance()); + } + + private static OAuth20Service githubService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl) + { + + ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl); + return builder.build(GitHubApi.instance()); + } + + private static OAuth20Service configurableService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl + ) { + ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl); + Log.d(TAG, "Creating ConfigurableApi"); + //Log.d(TAG, " authorize_url: " + cfg.get("authorize_url")); + //Log.d(TAG, " access_token_url: " + cfg.get("access_token_url")); + ConfigurableApi api = ConfigurableApi.instance() + .setAccessTokenEndpoint((String) cfg.get("access_token_url")) + .setAuthorizationBaseUrl((String) cfg.get("authorize_url")); + if (cfg.containsKey("access_token_verb")) { + //Log.d(TAG, " access_token_verb: " + cfg.get("access_token_verb")); + api.setAccessTokenVerb((String) cfg.get("access_token_verb")); + } + + return builder.build(api); + } + + private static OAuth20Service slackService( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl + ) { + + Log.d(TAG, "Make the builder: " + SlackApi.class); + ServiceBuilder builder = OAuthManagerProviders._oauth2ServiceBuilder(cfg, opts, callbackUrl); + return builder.build(SlackApi.instance()); + } + + private static ServiceBuilder _oauth2ServiceBuilder( + final HashMap cfg, + @Nullable final ReadableMap opts, + final String callbackUrl + ) { + String clientKey = (String) cfg.get("client_id"); + String clientSecret = (String) cfg.get("client_secret"); + String state; + if (cfg.containsKey("state")) { + state = (String) cfg.get("state"); + } else { + state = TAG + new Random().nextInt(999_999); + } + + // Builder + ServiceBuilder builder = new ServiceBuilder() + .apiKey(clientKey) + .apiSecret(clientSecret) + .state(state) + .debug(); + + String scopes = ""; + if (cfg.containsKey("scopes")) { + scopes = (String) cfg.get("scopes"); + String scopeStr = OAuthManagerProviders.getScopeString(scopes, ","); + builder.scope(scopeStr); + } + + if (opts != null && opts.hasKey("scopes")) { + scopes = (String) opts.getString("scopes"); + String scopeStr = OAuthManagerProviders.getScopeString(scopes, ","); + builder.scope(scopeStr); + } + + if (callbackUrl != null) { + builder.callback(callbackUrl); + } + + return builder; + } + + /** + * Convert a list of scopes by space or string into an array + */ + private static String getScopeString( + final String scopes, + final String joinBy + ) { + List array = Arrays.asList(scopes.replaceAll("\\s", "").split("[ ,]+")); + Log.d(TAG, "array: " + array + " (" + array.size() + ") from " + scopes); + return TextUtils.join(joinBy, array); + } +} diff --git a/android/src/main/java/io/fullstack/oauth/OAuthManagerStore.java b/android/src/main/java/io/fullstack/oauth/OAuthManagerStore.java new file mode 100644 index 0000000..bb2474d --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/OAuthManagerStore.java @@ -0,0 +1,104 @@ +package io.fullstack.oauth; + +import java.lang.reflect.Type; +import android.content.Context; +import android.util.Log; +import java.util.Set; +import java.util.HashMap; +import android.content.Context; +import android.net.Uri; +import android.os.Handler; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; +import com.google.gson.Gson; +import android.text.TextUtils; +import java.util.Collection; + +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.model.Token; +import com.google.gson.reflect.TypeToken; + +public class OAuthManagerStore { + private static final String TAG = "OAuthManagerStore"; + private static final String MAP_TAG = "CredentialList"; + // private static final Type MAP_TYPE = new TypeToken>() {}.getType(); + private static OAuthManagerStore oauthManagerStore; + private Context context; + + private SharedPreferences prefs; + private SharedPreferences.Editor editor; + private OnSharedPreferenceChangeListener listener; + + public OAuthManagerStore(Context ctx) { + this(ctx, TAG, Context.MODE_PRIVATE); + } + + public OAuthManagerStore(Context ctx, String name) { + this(ctx, name, Context.MODE_PRIVATE); + } + + public OAuthManagerStore(Context ctx, String name, int mode) { + // setup credential store + this.context = ctx; + this.prefs = ctx.getSharedPreferences(name, Context.MODE_PRIVATE); + editor = this.prefs.edit(); + listener = new SharedPreferences.OnSharedPreferenceChangeListener() { + @Override + public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { + Log.d(TAG, "Preferences changed: " + key); + } + }; + prefs.registerOnSharedPreferenceChangeListener(listener); + } + + public static OAuthManagerStore getOAuthManagerStore(Context ctx, String name, int mode) { + if (oauthManagerStore == null) { + oauthManagerStore = new OAuthManagerStore(ctx, name, mode); + } + return oauthManagerStore; + } + + public void store(String providerName, final OAuth1AccessToken accessToken) { + if (accessToken == null) { + throw new IllegalArgumentException("Token is null"); + } + if (providerName.equals("") || providerName == null) { + throw new IllegalArgumentException("Provider is null"); + } + editor.putString(providerName, new Gson().toJson(accessToken)); + } + + public void store(String providerName, final OAuth2AccessToken accessToken) { + if (accessToken == null) { + throw new IllegalArgumentException("Token is null"); + } + if (providerName.equals("") || providerName == null) { + throw new IllegalArgumentException("Provider is null"); + } + editor.putString(providerName, new Gson().toJson(accessToken)); + } + + + public void commit() { + editor.commit(); + } + + public T get(String providerName, Class a) { + String gson = this.prefs.getString(providerName, null); + if (gson == null) { + return null; + } else { + try { + return new Gson().fromJson(gson, a); + } catch (Exception ex) { + throw new IllegalArgumentException("Object storaged with key " + providerName + " is instanceof other class"); + } + } + } + + public void delete(String providerName) { + editor.remove(providerName); + this.commit(); + } +} \ No newline at end of file diff --git a/android/src/main/java/io/fullstack/oauth/services/ConfigurableApi.java b/android/src/main/java/io/fullstack/oauth/services/ConfigurableApi.java new file mode 100644 index 0000000..2019f53 --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/services/ConfigurableApi.java @@ -0,0 +1,66 @@ +package com.github.scribejava.apis; + +import android.util.Log; + +import com.github.scribejava.core.builder.api.DefaultApi20; +import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor; +import com.github.scribejava.core.extractors.TokenExtractor; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.model.Verb; + +public class ConfigurableApi extends DefaultApi20 { + + private String accessTokenEndpoint; + + private String authorizationBaseUrl; + + private Verb accessTokenVerb = Verb.GET; + + protected ConfigurableApi() { + } + + private static class InstanceHolder { + private static final ConfigurableApi INSTANCE = new ConfigurableApi(); + } + + public static ConfigurableApi instance() { + return InstanceHolder.INSTANCE; + } + + public ConfigurableApi setAccessTokenEndpoint(String endpoint) { + accessTokenEndpoint = endpoint; + return this; + } + + public ConfigurableApi setAuthorizationBaseUrl(String baseUrl) { + authorizationBaseUrl = baseUrl; + return this; + } + + public ConfigurableApi setAccessTokenVerb(String verb) { + if (verb.equalsIgnoreCase("GET")) { + accessTokenVerb = Verb.GET; + } else if (verb.equalsIgnoreCase("POST")) { + accessTokenVerb = Verb.POST; + } else { + Log.e("ConfigurableApi", "Expected GET or POST string values for accessTokenVerb."); + } + + return this; + } + + @Override + public Verb getAccessTokenVerb() { + return accessTokenVerb; + } + + @Override + public String getAccessTokenEndpoint() { + return accessTokenEndpoint; + } + + @Override + protected String getAuthorizationBaseUrl() { + return authorizationBaseUrl; + } +} diff --git a/android/src/main/java/io/fullstack/oauth/services/SlackApi.java b/android/src/main/java/io/fullstack/oauth/services/SlackApi.java new file mode 100644 index 0000000..b5fc61c --- /dev/null +++ b/android/src/main/java/io/fullstack/oauth/services/SlackApi.java @@ -0,0 +1,38 @@ +package com.github.scribejava.apis; + +import android.util.Log; + +import com.github.scribejava.core.builder.api.DefaultApi20; +import com.github.scribejava.core.extractors.OAuth2AccessTokenExtractor; +import com.github.scribejava.core.extractors.TokenExtractor; +import com.github.scribejava.core.model.OAuth2AccessToken; +import com.github.scribejava.core.model.Verb; + +public class SlackApi extends DefaultApi20 { + + protected SlackApi() { + } + + private static class InstanceHolder { + private static final SlackApi INSTANCE = new SlackApi(); + } + + public static SlackApi instance() { + return InstanceHolder.INSTANCE; + } + + @Override + public Verb getAccessTokenVerb() { + return Verb.GET; + } + + @Override + public String getAccessTokenEndpoint() { + return "https://slack.com/api/oauth.access"; + } + + @Override + protected String getAuthorizationBaseUrl() { + return "https://slack.com/oauth/authorize"; + } +} \ No newline at end of file diff --git a/android/src/main/res/layout/webview_layout.xml b/android/src/main/res/layout/webview_layout.xml new file mode 100644 index 0000000..29f061f --- /dev/null +++ b/android/src/main/res/layout/webview_layout.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/bin/cocoapods.sh b/bin/cocoapods.sh index 4605a67..7a474ae 100755 --- a/bin/cocoapods.sh +++ b/bin/cocoapods.sh @@ -3,7 +3,7 @@ ## https://github.com/auth0/react-native-lock/blob/master/bin/cocoapods.sh ios_dir=`pwd`/ios -if [ -d ios_dir ] +if [ ! -d $ios_dir ] then exit 0 fi @@ -45,4 +45,4 @@ cd .. echo "Installing Pods" -pod install --project-directory=ios \ No newline at end of file +pod install --project-directory=ios diff --git a/ios/OAuthManager.xcodeproj/project.pbxproj b/ios/OAuthManager.xcodeproj/project.pbxproj index 6580450..9929888 100644 --- a/ios/OAuthManager.xcodeproj/project.pbxproj +++ b/ios/OAuthManager.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + B76E094E1E768CE100A9AF9A /* README.md in Sources */ = {isa = PBXBuildFile; fileRef = B76E094B1E768CE100A9AF9A /* README.md */; }; + B76E094F1E768CE100A9AF9A /* XMLReader.h in Headers */ = {isa = PBXBuildFile; fileRef = B76E094C1E768CE100A9AF9A /* XMLReader.h */; }; + B76E09501E768CE100A9AF9A /* XMLReader.m in Sources */ = {isa = PBXBuildFile; fileRef = B76E094D1E768CE100A9AF9A /* XMLReader.m */; }; D935004D1D513CF700C7BA47 /* OAuthManager.m in Sources */ = {isa = PBXBuildFile; fileRef = D935004C1D513CF700C7BA47 /* OAuthManager.m */; }; D9F2EAD31DA9A9650000BD52 /* OAuthClient.h in Headers */ = {isa = PBXBuildFile; fileRef = D9F2EAD11DA9A9650000BD52 /* OAuthClient.h */; }; D9F2EAD41DA9A9650000BD52 /* OAuthClient.m in Sources */ = {isa = PBXBuildFile; fileRef = D9F2EAD21DA9A9650000BD52 /* OAuthClient.m */; }; @@ -65,6 +68,9 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + B76E094B1E768CE100A9AF9A /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + B76E094C1E768CE100A9AF9A /* XMLReader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XMLReader.h; sourceTree = ""; }; + B76E094D1E768CE100A9AF9A /* XMLReader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = XMLReader.m; sourceTree = ""; }; D91353961DA7849100AABC96 /* libOAuthManager.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libOAuthManager.a; sourceTree = BUILT_PRODUCTS_DIR; }; D935004C1D513CF700C7BA47 /* OAuthManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OAuthManager.m; path = OAuthManager/OAuthManager.m; sourceTree = ""; }; D991AB771D1B237400DE9E58 /* Pods.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Pods.xcodeproj; path = Pods/Pods.xcodeproj; sourceTree = ""; }; @@ -91,9 +97,21 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + B76E094A1E768CE100A9AF9A /* XMLReader */ = { + isa = PBXGroup; + children = ( + B76E094B1E768CE100A9AF9A /* README.md */, + B76E094C1E768CE100A9AF9A /* XMLReader.h */, + B76E094D1E768CE100A9AF9A /* XMLReader.m */, + ); + name = XMLReader; + path = OAuthManager/XMLReader; + sourceTree = SOURCE_ROOT; + }; D93EF9941DA77CBB00EC55A0 /* /Users/auser/Development/react-native/mine/OAuthManager/ios/OAuthManager.xcodeproj */ = { isa = PBXGroup; children = ( + B76E094A1E768CE100A9AF9A /* XMLReader */, D9F2EADF1DA9A9930000BD52 /* OAuthManager.h */, D9F2EAE01DA9A9930000BD52 /* OAuthManager.m */, D9F2EAE11DA9A9930000BD52 /* OAuthManagerConstants.h */, @@ -143,6 +161,7 @@ files = ( D9F2EAD91DA9A9730000BD52 /* OAuth1Client.h in Headers */, D9F2EADE1DA9A9840000BD52 /* OAuthClientProtocol.h in Headers */, + B76E094F1E768CE100A9AF9A /* XMLReader.h in Headers */, D9F2EADB1DA9A9730000BD52 /* OAuth2Client.h in Headers */, D9F2EAD31DA9A9650000BD52 /* OAuthClient.h in Headers */, D9F2EAE41DA9A9930000BD52 /* OAuthManagerConstants.h in Headers */, @@ -232,11 +251,13 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + B76E094E1E768CE100A9AF9A /* README.md in Sources */, D9F2EADC1DA9A9730000BD52 /* OAuth2Client.m in Sources */, D935004D1D513CF700C7BA47 /* OAuthManager.m in Sources */, D9F2EAD41DA9A9650000BD52 /* OAuthClient.m in Sources */, D9F2EADA1DA9A9730000BD52 /* OAuth1Client.m in Sources */, D9F2EAE31DA9A9930000BD52 /* OAuthManager.m in Sources */, + B76E09501E768CE100A9AF9A /* XMLReader.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -275,7 +296,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = NO; EMBEDDED_CONTENT_CONTAINS_SWIFT = NO; - ENABLE_BITCODE = NO; + ENABLE_BITCODE = YES; ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_TESTABILITY = YES; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -328,7 +349,7 @@ CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; COPY_PHASE_STRIP = YES; EMBEDDED_CONTENT_CONTAINS_SWIFT = NO; - ENABLE_BITCODE = NO; + ENABLE_BITCODE = YES; ENABLE_NS_ASSERTIONS = NO; ENABLE_STRICT_OBJC_MSGSEND = YES; GCC_C_LANGUAGE_STANDARD = gnu99; @@ -361,7 +382,7 @@ CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES; DYLIB_INSTALL_NAME_BASE = ""; EMBEDDED_CONTENT_CONTAINS_SWIFT = NO; - ENABLE_BITCODE = NO; + ENABLE_BITCODE = YES; HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../../React/**", @@ -388,7 +409,7 @@ CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES; DYLIB_INSTALL_NAME_BASE = ""; EMBEDDED_CONTENT_CONTAINS_SWIFT = NO; - ENABLE_BITCODE = NO; + ENABLE_BITCODE = YES; HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../../React/**", diff --git a/ios/OAuthManager.xcodeproj/project.xcworkspace/xcuserdata/auser.xcuserdatad/UserInterfaceState.xcuserstate b/ios/OAuthManager.xcodeproj/project.xcworkspace/xcuserdata/auser.xcuserdatad/UserInterfaceState.xcuserstate index 88d68a1..07c1b82 100644 Binary files a/ios/OAuthManager.xcodeproj/project.xcworkspace/xcuserdata/auser.xcuserdatad/UserInterfaceState.xcuserstate and b/ios/OAuthManager.xcodeproj/project.xcworkspace/xcuserdata/auser.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/ios/OAuthManager/OAuth1Client.m b/ios/OAuthManager/OAuth1Client.m index 97ff342..b570104 100644 --- a/ios/OAuthManager/OAuth1Client.m +++ b/ios/OAuthManager/OAuth1Client.m @@ -31,13 +31,17 @@ - (void) authorizeWithUrl:(NSString *)providerName __weak id client = self; [account authenticateWithHandler:^(NSArray *responses, NSError *error) { - [client clearPendingAccount]; - if (error != nil) { - onError(error); + NSString *response = ((DCTAuthResponse *)responses[0]).responseDescription; + NSError *err = [NSError errorWithDomain:error.domain + code:error.code + userInfo:@{@"response": response}]; + onError(err); return; } + [client clearPendingAccount]; + if (!account.authorized) { NSError *err = QUICK_ERROR(E_ACCOUNT_NOT_AUTHORIZED, @"account not authorized"); onError(err); diff --git a/ios/OAuthManager/OAuth2Client.m b/ios/OAuthManager/OAuth2Client.m index 3779e35..092a255 100644 --- a/ios/OAuthManager/OAuth2Client.m +++ b/ios/OAuthManager/OAuth2Client.m @@ -34,13 +34,17 @@ - (void) authorizeWithUrl:(NSString *)providerName // authorizeWithClientID [account authenticateWithHandler:^(NSArray *responses, NSError *error) { NSLog(@"authenticateWithHandler: %@", responses); - [client clearPendingAccount]; if (error != nil) { - NSLog(@"Some error: %@", error); - onError(error); + NSString *response = ((DCTAuthResponse *)responses[0]).responseDescription; + NSError *err = [NSError errorWithDomain:error.domain + code:error.code + userInfo:@{@"response": response}]; + onError(err); return; } + + [client clearPendingAccount]; if (!account.authorized) { NSError *err = QUICK_ERROR(E_ACCOUNT_NOT_AUTHORIZED, @"account not authorized"); @@ -63,7 +67,10 @@ - (void) reauthenticateWithHandler:(NSString *) providerName [account reauthenticateWithHandler:^(DCTAuthResponse *response, NSError *error) { NSLog(@"Reauthenticating..."); if (error != nil) { - onError(error); + NSError *err = [NSError errorWithDomain:error.domain + code:error.code + userInfo:@{@"response": response.responseDescription}]; + onError(err); return; } @@ -86,7 +93,12 @@ - (DCTOAuth2Account *) getAccount:(NSString *)providerName // Required NSURL *authorize_url = [cfg objectForKey:@"authorize_url"]; NSString *scopeStr = [cfg valueForKey:@"scopes"]; - NSArray *scopes = [scopeStr componentsSeparatedByString:@","]; + // NSArray *scopes = [scopeStr componentsSeparatedByString:@","]; + + NSString *sep = @", "; + NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep]; + NSArray *scopes = [scopeStr componentsSeparatedByCharactersInSet:set]; + // Optional NSURL *access_token_url = [cfg objectForKey:@"access_token_url"]; @@ -95,6 +107,7 @@ - (DCTOAuth2Account *) getAccount:(NSString *)providerName NSString *username = [cfg valueForKey:@"username"]; NSString *password = [cfg valueForKey:@"password"]; + NSLog(@"getAccount config: %@", cfg); if (access_token_url != nil) { account = [[DCTOAuth2Account alloc] initWithType:providerName authorizeURL:authorize_url diff --git a/ios/OAuthManager/OAuthClient.m b/ios/OAuthManager/OAuthClient.m index 9120dd9..b1e18f5 100644 --- a/ios/OAuthManager/OAuthClient.m +++ b/ios/OAuthManager/OAuthClient.m @@ -30,7 +30,12 @@ - (void) reauthenticateWithHandler:(NSString *) providerName - (void) cancelAuthentication { if (_account != nil) { - [_account cancelAuthentication]; + @try { + [_account cancelAuthentication]; + } + @catch (NSException *exception) { + NSLog(@"An exception occurred while cancelling authentication: %@", [exception reason]); + } } } @@ -43,7 +48,9 @@ - (void) savePendingAccount:(DCTAuthAccount *) account - (void) clearPendingAccount { - _account = nil; + NSLog(@"called clearPendingAccount: %@", _account); + [_account cancelAuthentication]; + _account = nil; } - (void (^)(DCTAuthResponse *response, NSError *error)) getHandler:(DCTAuthAccount *) account diff --git a/ios/OAuthManager/OAuthManager.h b/ios/OAuthManager/OAuthManager.h index e0df954..cdefdda 100644 --- a/ios/OAuthManager/OAuthManager.h +++ b/ios/OAuthManager/OAuthManager.h @@ -7,8 +7,19 @@ #import -#import "RCTBridgeModule.h" -#import "RCTLinkingManager.h" +#if __has_include() + #import +#else + #import "RCTBridgeModule.h" +#endif + +#if __has_include() + #import +#else + #import "RCTLinkingManager.h" +#endif + + @class OAuthClient; diff --git a/ios/OAuthManager/OAuthManager.m b/ios/OAuthManager/OAuthManager.m index fb2442e..2e51cea 100644 --- a/ios/OAuthManager/OAuthManager.m +++ b/ios/OAuthManager/OAuthManager.m @@ -6,6 +6,8 @@ #import #import +#import +#import #import "OAuthManager.h" #import "DCTAuth.h" @@ -14,27 +16,42 @@ #import "OAuthClient.h" #import "OAuth1Client.h" #import "OAuth2Client.h" +#import "XMLReader.h" @interface OAuthManager() - @property (nonatomic) NSArray *pendingClients; - @property BOOL pendingAuthentication; +@property (nonatomic) NSArray *pendingClients; +@property BOOL pendingAuthentication; @end @implementation OAuthManager @synthesize callbackUrls = _callbackUrls; +static NSString *const AUTH_MANAGER_TAG = @"AUTH_MANAGER"; +static OAuthManager *manager; +static dispatch_once_t onceToken; +static SFSafariViewController *safariViewController = nil; + RCT_EXPORT_MODULE(OAuthManager); +// Run on a different thread +- (dispatch_queue_t)methodQueue +{ + return dispatch_queue_create("io.fullstack.oauth", DISPATCH_QUEUE_SERIAL); +} + + (instancetype)sharedManager { - static OAuthManager *manager; - static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager = [self new]; }); return manager; } ++ (void) reset { + onceToken = nil; + manager = nil; +} + - (instancetype) init { self = [super init]; if (self != nil) { @@ -45,7 +62,7 @@ - (instancetype) init { selector:@selector(didBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; - + } return self; } @@ -57,10 +74,7 @@ - (void) dealloc - (void) didBecomeActive:(NSNotification *)notification { - NSLog(@"Application reopened: %@", @(self.pendingAuthentication)); - for (OAuthClient *client in _pendingClients) { - [self removePending:client]; - } + // TODO? } /* @@ -72,8 +86,18 @@ + (BOOL)setupOAuthHandler:(UIApplication *)application DCTAuthPlatform *authPlatform = [DCTAuthPlatform sharedPlatform]; [authPlatform setURLOpener: ^void(NSURL *URL, DCTAuthPlatformCompletion completion) { - [sharedManager setPendingAuthentication:YES]; - [application openURL:URL]; + // [sharedManager setPendingAuthentication:YES]; + if ([SFSafariViewController class] != nil) { + dispatch_async(dispatch_get_main_queue(), ^{ + safariViewController = [[SFSafariViewController alloc] initWithURL:URL]; + UIViewController *viewController = application.keyWindow.rootViewController; + dispatch_async(dispatch_get_main_queue(), ^{ + [viewController presentViewController:safariViewController animated:YES completion: nil]; + }); + }); + } else { + [application openURL:URL]; + } completion(YES); }]; @@ -98,15 +122,15 @@ + (BOOL)handleOpenUrl:(UIApplication *)application openURL:(NSURL *)url { OAuthManager *manager = [OAuthManager sharedManager]; NSString *strUrl = [manager stringHost:url]; - - NSLog(@"Handling handleOpenUrl: %@", strUrl); if ([manager.callbackUrls indexOfObject:strUrl] != NSNotFound) { + if(safariViewController != nil) { + [safariViewController dismissViewControllerAnimated:YES completion:nil]; + } return [DCTAuth handleURL:url]; } - - [manager clearPending]; + // [manager clearPending]; return [RCTLinkingManager application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; @@ -126,23 +150,25 @@ - (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)c NSMutableArray *arr = [_callbackUrls mutableCopy]; NSString *callbackUrlStr = [config valueForKey:@"callback_url"]; NSURL *callbackUrl = [NSURL URLWithString:callbackUrlStr]; - NSString *saveCallbackUrl = [self stringHost:callbackUrl]; - [arr addObject:saveCallbackUrl]; - _callbackUrls = [arr copy]; + NSString *saveCallbackUrl = [[self stringHost:callbackUrl] lowercaseString]; + + if ([arr indexOfObject:saveCallbackUrl] == NSNotFound) { + [arr addObject:saveCallbackUrl]; + _callbackUrls = [arr copy]; + NSLog(@"Saved callback url: %@ in %@", saveCallbackUrl, _callbackUrls); + } + // Convert objects of url type for (NSString *name in [config allKeys]) { if ([name rangeOfString:@"_url"].location != NSNotFound) { // This is a URL representation NSString *urlStr = [config valueForKey:name]; - NSURL *url = [NSURL URLWithString:[urlStr - stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; + NSURL *url = [NSURL URLWithString:urlStr]; [objectProps setObject:url forKey:name]; } else { NSString *str = [NSString stringWithString:[config valueForKey:name]]; - NSString *escapedStr = [str - stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; - [objectProps setValue:[escapedStr copy] forKey:name]; + [objectProps setValue:str forKey:name]; } } @@ -183,7 +209,91 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name } } -#pragma mark OAuth1.0 +#pragma mark OAuth + +// TODO: Remove opts +RCT_EXPORT_METHOD(getSavedAccounts:(NSDictionary *) opts + callback:(RCTResponseSenderBlock) callback) +{ + OAuthManager *manager = [OAuthManager sharedManager]; + DCTAuthAccountStore *store = [manager accountStore]; + + NSSet *accounts = [store accounts]; + NSMutableArray *respAccounts = [[NSMutableArray alloc] init]; + for (DCTAuthAccount *account in [accounts allObjects]) { + NSString *providerName = account.type; + NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy]; + NSMutableDictionary *acc = [[manager getAccountResponse:account cfg:cfg] mutableCopy]; + [acc setValue:providerName forKey:@"provider"]; + [respAccounts addObject:acc]; + } + callback(@[[NSNull null], @{ + @"status": @"ok", + @"accounts": respAccounts + }]); +} + +// TODO: Remove opts +RCT_EXPORT_METHOD(getSavedAccount:(NSString *)providerName + opts:(NSDictionary *) opts + callback:(RCTResponseSenderBlock)callback) +{ + OAuthManager *manager = [OAuthManager sharedManager]; + NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy]; + + DCTAuthAccount *existingAccount = [manager accountForProvider:providerName]; + if (existingAccount != nil) { + if ([existingAccount isAuthorized]) { + NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg]; + callback(@[[NSNull null], @{ + @"status": @"ok", + @"provider": providerName, + @"response": accountResponse + }]); + return; + } else { + DCTAuthAccountStore *store = [manager accountStore]; + [store deleteAccount:existingAccount]; + NSDictionary *errResp = @{ + @"status": @"error", + @"response": @{ + @"msg": @"Account not authorized" + } + }; + callback(@[errResp]); + } + } else { + NSDictionary *errResp = @{ + @"status": @"error", + @"response": @{ + @"msg": @"No saved account" + } + }; + callback(@[errResp]); + } +} + +RCT_EXPORT_METHOD(deauthorize:(NSString *) providerName + callback:(RCTResponseSenderBlock) callback) +{ + OAuthManager *manager = [OAuthManager sharedManager]; + DCTAuthAccountStore *store = [manager accountStore]; + + DCTAuthAccount *existingAccount = [manager accountForProvider:providerName]; + if (existingAccount != nil) { + [store deleteAccount:existingAccount]; + callback(@[[NSNull null], @{ + @"status": @"ok" + }]); + } else { + NSDictionary *resp = @{ + @"status": @"error", + @"msg": [NSString stringWithFormat:@"No account found for %@", providerName] + }; + callback(@[resp]); + } +} + /** * authorize with url * provider, url, scope, state, params @@ -193,9 +303,27 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name callback:(RCTResponseSenderBlock)callback) { OAuthManager *manager = [OAuthManager sharedManager]; + [manager clearPending]; NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy]; - NSString *appName = [cfg valueForKey:@"app_name"]; + DCTAuthAccount *existingAccount = [manager accountForProvider:providerName]; + NSString *clientID = ([providerName isEqualToString:@"google"]) ? ((DCTOAuth2Credential *) existingAccount).clientID : (NSString *)nil; + + if (([providerName isEqualToString:@"google"] && existingAccount && clientID != nil) + || (![providerName isEqualToString:@"google"] && existingAccount != nil)) { + if ([existingAccount isAuthorized]) { + NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg]; + callback(@[[NSNull null], @{ + @"status": @"ok", + @"provider": providerName, + @"response": accountResponse + }]); + return; + } else { + DCTAuthAccountStore *store = [manager accountStore]; + [store deleteAccount:existingAccount]; + } + } NSString *callbackUrl; NSURL *storedCallbackUrl = [cfg objectForKey:@"callback_url"]; @@ -203,6 +331,7 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name if (storedCallbackUrl != nil) { callbackUrl = [storedCallbackUrl absoluteString]; } else { + NSString *appName = [cfg valueForKey:@"app_name"]; callbackUrl = [NSString stringWithFormat:@"%@://oauth-response/%@", appName, @@ -220,27 +349,29 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name } else if ([version isEqualToString:@"2.0"]) { client = (OAuthClient *)[[OAuth2Client alloc] init]; } else { - NSLog(@"Provider number: %@", version); return callback(@[@{ @"status": @"error", @"msg": @"Unknown provider" }]); } - + // Store pending client - [self addPending:client]; - _pendingAuthentication = YES; + [manager addPending:client]; + _pendingAuthentication = YES; + + NSLog(@"Calling authorizeWithUrl: %@ with callbackURL: %@\n %@", providerName, callbackUrl, cfg); [client authorizeWithUrl:providerName url:callbackUrl cfg:cfg - onSuccess:^(DCTAuthAccount *account) { - NSLog(@"authorizeWithUrl: %@", account); + NSLog(@"on success called with account: %@", account); NSDictionary *accountResponse = [manager getAccountResponse:account cfg:cfg]; _pendingAuthentication = NO; [manager removePending:client]; + [[manager accountStore] saveAccount:account]; // <~ + callback(@[[NSNull null], @{ @"status": @"ok", @"response": accountResponse @@ -248,19 +379,242 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name } onError:^(NSError *error) { NSLog(@"Error in authorizeWithUrl: %@", error); _pendingAuthentication = NO; - [manager removePending:client]; callback(@[@{ @"status": @"error", - @"msg": [error localizedDescription] + @"msg": [error localizedDescription], + @"userInfo": error.userInfo }]); + [manager removePending:client]; }]; } +RCT_EXPORT_METHOD(makeRequest:(NSString *)providerName + urlOrPath:(NSString *) urlOrPath + opts:(NSDictionary *) opts + callback:(RCTResponseSenderBlock)callback) +{ + OAuthManager *manager = [OAuthManager sharedManager]; + NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy]; + + DCTAuthAccount *existingAccount = [manager accountForProvider:providerName]; + if (existingAccount == nil) { + NSDictionary *errResp = @{ + @"status": @"error", + @"msg": [NSString stringWithFormat:@"No account found for %@", providerName] + }; + callback(@[errResp]); + return; + } + + NSDictionary *creds = [self credentialForAccount:providerName cfg:cfg]; + + // If we have the http in the string, use it as the URL, otherwise create one + // with the configuration + NSURL *apiUrl; + if ([urlOrPath hasPrefix:@"http"]) { + apiUrl = [NSURL URLWithString:urlOrPath]; + } else { + NSURL *apiHost = [cfg objectForKey:@"api_url"]; + apiUrl = [NSURL URLWithString:[[apiHost absoluteString] stringByAppendingString:urlOrPath]]; + } + + // If there are params + NSMutableArray *items = [NSMutableArray array]; + NSDictionary *params = [opts objectForKey:@"params"]; + if (params != nil) { + for (NSString *key in params) { + + NSString *value = [params valueForKey:key]; + + if ([value isEqualToString:@"access_token"]) { + value = [creds valueForKey:@"access_token"]; + } + + NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:value]; + + if (item != nil) { + [items addObject:item]; + } + } + } + + NSString *methodStr = [opts valueForKey:@"method"]; + + DCTAuthRequestMethod method = [manager getRequestMethodByString:methodStr]; + + DCTAuthRequest *request = + [[DCTAuthRequest alloc] + initWithRequestMethod:method + URL:apiUrl + items:items]; + + // Allow json body in POST / PUT requests + NSDictionary *body = [opts objectForKey:@"body"]; + if (body != nil) { + NSMutableArray *items = [NSMutableArray array]; + + for (NSString *key in body) { + NSString *value = [body valueForKey:key]; + + DCTAuthContentItem *item = [[DCTAuthContentItem alloc] initWithName:key value:value]; + + if(item != nil) { + [items addObject: item]; + } + } + + DCTAuthContent *content = [[DCTAuthContent alloc] initWithEncoding:NSUTF8StringEncoding + type:DCTAuthContentTypeJSON + items:items]; + [request setContent:content]; + } + + // If there are headers + NSDictionary *headers = [opts objectForKey:@"headers"]; + if (headers != nil) { + NSMutableDictionary *existingHeaders = [request.HTTPHeaders mutableCopy]; + for (NSString *header in headers) { + [existingHeaders setValue:[headers valueForKey:header] forKey:header]; + } + request.HTTPHeaders = existingHeaders; + } + + request.account = existingAccount; + + [request performRequestWithHandler:^(DCTAuthResponse *response, NSError *error) { + if (error != nil) { + NSDictionary *errorDict = @{ + @"status": @"error", + @"msg": [error localizedDescription] + }; + callback(@[errorDict]); + } else { + NSInteger statusCode = response.statusCode; + NSData *rawData = response.data; + NSDictionary *headers = response.HTTPHeaders; + + NSError *err; + NSArray *data; + + + + // Check if returned data is a valid JSON + // != nil returned if the rawdata is not a valid JSON + if ((data = [NSJSONSerialization JSONObjectWithData:rawData + options:kNilOptions + error:&err]) == nil) { + // Resetting err variable. + err = nil; + + // Parse XML + data = [XMLReader dictionaryForXMLData:rawData + options:XMLReaderOptionsProcessNamespaces + error:&err]; + } + if (err != nil) { + NSDictionary *errResp = @{ + @"status": @"error", + @"msg": [NSString stringWithFormat:@"JSON parsing error: %@", [err localizedDescription]] + }; + callback(@[errResp]); + } else { + + NSDictionary *resp = @{ + @"status": @(statusCode), + @"data": data != nil ? data : @[], + @"headers": headers, + }; + callback(@[[NSNull null], resp]); + } + } + }]; +} + #pragma mark - private +- (DCTAuthAccount *) accountForProvider:(NSString *) providerName +{ + DCTAuthAccountStore *store = [self accountStore]; + NSSet *accounts = [store accountsWithType:providerName]; + if ([accounts count] == 0) { + return nil; + } else { + NSArray *allAccounts = [accounts allObjects]; + if ([allAccounts count] == 0) { + return nil; + } else { + return [allAccounts lastObject]; + } + } +} + +- (NSDictionary *) credentialForAccount:(NSString *)providerName + cfg:(NSDictionary *)cfg +{ + DCTAuthAccount *account = [self accountForProvider:providerName]; + if (!account) { + return nil; + } + + NSString *version = [cfg valueForKey:@"auth_version"]; + NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; + + if ([version isEqualToString:@"1.0"]) { + DCTOAuth1Credential *credentials = [account credential]; + + if (credentials) { + if (credentials.oauthToken) { + NSString *token = credentials.oauthToken; + [dict setObject:token forKey:@"access_token"]; + } + + if (credentials.oauthTokenSecret) { + NSString *secret = credentials.oauthTokenSecret; + [dict setObject:secret forKey:@"access_token_secret"]; + } + } + + } else if ([version isEqualToString:@"2.0"]) { + DCTOAuth2Credential *credentials = [account credential]; + + if (credentials) { + if (credentials.accessToken) { + NSString *token = credentials.accessToken; + [dict setObject:token forKey:@"access_token"]; + } + } + } + + return dict; +} + +- (DCTAuthRequestMethod) getRequestMethodByString:(NSString *) method +{ + if ([method compare:@"get" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodGET; + } else if ([method compare:@"post" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodPOST; + } else if ([method compare:@"put" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodPUT; + } else if ([method compare:@"delete" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodDELETE; + } else if ([method compare:@"head" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodHEAD; + } else if ([method compare:@"options" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodOPTIONS; + } else if ([method compare:@"patch" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodPATCH; + } else if ([method compare:@"trace" options:NSCaseInsensitiveSearch] == NSOrderedSame) { + return DCTAuthRequestMethodTRACE; + } else { + return DCTAuthRequestMethodGET; + } +} + - (NSDictionary *) getAccountResponse:(DCTAuthAccount *) account cfg:(NSDictionary *)cfg { + NSArray *ignoredCredentialProperties = @[@"superclass", @"hash", @"description", @"debugDescription"]; NSString *version = [cfg valueForKey:@"auth_version"]; NSMutableDictionary *accountResponse = [@{ @"authorized": @(account.authorized), @@ -269,25 +623,67 @@ - (NSDictionary *) getAccountResponse:(DCTAuthAccount *) account if ([version isEqualToString:@"1.0"]) { DCTOAuth1Credential *credential = account.credential; - NSDictionary *cred = @{ - @"oauth_token": credential.oauthToken, - @"oauth_secret": credential.oauthTokenSecret - }; - [accountResponse setObject:cred forKey:@"credentials"]; + if (credential != nil) { + NSDictionary *cred = @{ + @"access_token": credential.oauthToken, + @"access_token_secret": credential.oauthTokenSecret + }; + [accountResponse setObject:cred forKey:@"credentials"]; + } } else if ([version isEqualToString:@"2.0"]) { DCTOAuth2Credential *credential = account.credential; - NSMutableDictionary *cred = [@{ - @"access_token": credential.accessToken, - @"type": @(credential.type) - } mutableCopy]; - if (credential.refreshToken != nil) { - [cred setValue:credential.refreshToken forKey:@"refresh_token"]; + if (credential != nil) { + + NSMutableDictionary *cred = [self dictionaryForCredentialKeys: credential]; + + DCTOAuth2Account *oauth2Account = (DCTOAuth2Account *) account; + if (oauth2Account.scopes) { + [cred setObject:oauth2Account.scopes forKey:@"scopes"]; + } + + [accountResponse setObject:cred forKey:@"credentials"]; } - [accountResponse setObject:cred forKey:@"credentials"]; } + [accountResponse setValue:[account identifier] forKey:@"identifier"]; + if (account.userInfo != nil) { + [accountResponse setObject:[account userInfo] forKey:@"user_info"]; + } + return accountResponse; } +- (NSDictionary *) dictionaryForCredentialKeys:(NSObject *) credential +{ + NSArray *ignoredCredentialProperties = @[@"superclass", @"hash", @"description", @"debugDescription"]; + unsigned int count = 0; + NSMutableDictionary *cred = [NSMutableDictionary new]; + objc_property_t *properties = class_copyPropertyList([credential class], &count); + + for (int i = 0; i < count; i++) { + + NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])]; + if ([ignoredCredentialProperties containsObject:key]) { + NSLog(@"Ignoring credentials key: %@", key); + } else { + id value = [credential valueForKey:key]; + + if (value == nil) { + + } else if ([value isKindOfClass:[NSNumber class]] + || [value isKindOfClass:[NSString class]] + || [value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSMutableArray class]]) { + // TODO: extend to other types + [cred setObject:value forKey:key]; + } else if ([value isKindOfClass:[NSObject class]]) { + [cred setObject:[value dictionary] forKey:key]; + } else { + NSLog(@"Invalid type for %@ (%@)", NSStringFromClass([self class]), key); + } + } + } + return cred; +} + - (void) clearPending { OAuthManager *manager = [OAuthManager sharedManager]; @@ -295,6 +691,7 @@ - (void) clearPending [manager removePending:client]; } manager.pendingClients = [NSArray array]; + _pendingAuthentication = NO; } - (void) addPending:(OAuthClient *) client @@ -307,17 +704,30 @@ - (void) addPending:(OAuthClient *) client - (void) removePending:(OAuthClient *) client { + [client clearPendingAccount]; OAuthManager *manager = [OAuthManager sharedManager]; NSUInteger idx = [manager.pendingClients indexOfObject:client]; - NSMutableArray *newPendingClients = [manager.pendingClients mutableCopy]; - [newPendingClients removeObjectAtIndex:idx]; - [client cancelAuthentication]; - manager.pendingClients = newPendingClients; + if ([manager.pendingClients count] <= idx) { + NSMutableArray *newPendingClients = [manager.pendingClients mutableCopy]; + [newPendingClients removeObjectAtIndex:idx]; + manager.pendingClients = newPendingClients; + } +} + +- (DCTAuthAccountStore *) accountStore +{ + NSString *name = [NSString stringWithFormat:@"%@", AUTH_MANAGER_TAG]; + return [DCTAuthAccountStore accountStoreWithName:name]; } - (NSString *) stringHost:(NSURL *)url { - NSString *str = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.path]; + NSString *str; + if (url.host != nil) { + str = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.path]; + } else { + str = [NSString stringWithFormat:@"%@%@", url.scheme, url.path]; + } if ([str hasSuffix:@"/"]) { str = [str substringToIndex:str.length - 1]; @@ -327,3 +737,4 @@ - (NSString *) stringHost:(NSURL *)url } @end + diff --git a/ios/OAuthManager/XMLReader/README.md b/ios/OAuthManager/XMLReader/README.md new file mode 100755 index 0000000..634d324 --- /dev/null +++ b/ios/OAuthManager/XMLReader/README.md @@ -0,0 +1,77 @@ +# XMLReader + +This project comes from a component developed by Troy Brant and published on his website : http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/ + +I'm open sourcing some of the updates I've made on it. + + +## Usage + + NSData *data = ...; // some data that can be received from remote service + NSError *error = nil; + NSDictionary *dict = [XMLReader dictionaryForXMLData:data + options:XMLReaderOptionsProcessNamespaces + error:&error]; + + +## Requirements + +Xcode 4.4 and above because project use the "auto-synthesized property" feature. + + +## FAQ + +#### Sometimes I get an `NSDictionary` while I must get an `NSArray`, why ? + +In the algorithm of the `XMLReader`, when the parser found a new tag it automatically creates an `NSDictionary`, if it found another occurrence of the same tag at the same level in the XML tree it creates another dictionary and put both dictionaries inside an `NSArray`. + +The consequence is: if you have a list that contains only one item, you will get an `NSDictionary` as result and not an `NSArray`. +The only workaround is to check the class of the object contained for in the dictionary using `isKindOfClass:`. See sample code below : + + NSData *data = ...; + NSError *error = nil; + NSDictionary *dict = [XMLReader dictionaryForXMLData:data error:&error]; + + NSArray *list = [dict objectForKey:@"list"]; + if (![list isKindOfClass:[NSArray class]]) + { + // if 'list' isn't an array, we create a new array containing our object + list = [NSArray arrayWithObject:list]; + } + + // we can loop through items safely now + for (NSDictionary *item in list) + { + // ... + } + + +#### I don't have enable ARC on my project, how can I use your library ? + +You have 2 options: + +* Use the branch "[no-objc-arc](https://github.com/amarcadet/XMLReader/tree/no-objc-arc)" that use manual reference counting. +* **Better choice:** add the "-fobjc-arc" compiler flag on `XMLReader.m` file in your build phases. + +#### I have trust issues, I don't want ARC, I prefer MRC, what can I do ? + +Well, nobody is perfect but, still, you can use the branch "[no-objc-arc](https://github.com/amarcadet/XMLReader/tree/no-objc-arc)". + + +## Contributions + +Thanks to the original author of this component Troy Brant and to [Divan "snip3r8" Visagie](https://github.com/snip3r8) for providing ARC support. + + +## License + +Copyright (C) 2012 Antoine Marcadet + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/amarcadet/XMLReader/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + diff --git a/ios/OAuthManager/XMLReader/XMLReader.h b/ios/OAuthManager/XMLReader/XMLReader.h new file mode 100755 index 0000000..aeca0b8 --- /dev/null +++ b/ios/OAuthManager/XMLReader/XMLReader.h @@ -0,0 +1,25 @@ +// +// XMLReader.h +// +// Created by Troy Brant on 9/18/10. +// Updated by Antoine Marcadet on 9/23/11. +// Updated by Divan Visagie on 2012-08-26 +// + +#import + +enum { + XMLReaderOptionsProcessNamespaces = 1 << 0, // Specifies whether the receiver reports the namespace and the qualified name of an element. + XMLReaderOptionsReportNamespacePrefixes = 1 << 1, // Specifies whether the receiver reports the scope of namespace declarations. + XMLReaderOptionsResolveExternalEntities = 1 << 2, // Specifies whether the receiver reports declarations of external entities. +}; +typedef NSUInteger XMLReaderOptions; + +@interface XMLReader : NSObject + ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer; ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer; ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data options:(XMLReaderOptions)options error:(NSError **)errorPointer; ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string options:(XMLReaderOptions)options error:(NSError **)errorPointer; + +@end diff --git a/ios/OAuthManager/XMLReader/XMLReader.m b/ios/OAuthManager/XMLReader/XMLReader.m new file mode 100755 index 0000000..754c95a --- /dev/null +++ b/ios/OAuthManager/XMLReader/XMLReader.m @@ -0,0 +1,176 @@ +// +// XMLReader.m +// +// Created by Troy Brant on 9/18/10. +// Updated by Antoine Marcadet on 9/23/11. +// Updated by Divan Visagie on 2012-08-26 +// + +#import "XMLReader.h" + +#if !defined(__has_feature) || !__has_feature(objc_arc) +#error "XMLReader requires ARC support." +#endif + +NSString *const kXMLReaderTextNodeKey = @"text"; +NSString *const kXMLReaderAttributePrefix = @"@"; + +@interface XMLReader () + +@property (nonatomic, strong) NSMutableArray *dictionaryStack; +@property (nonatomic, strong) NSMutableString *textInProgress; +@property (nonatomic, strong) NSError *errorPointer; + +@end + + +@implementation XMLReader + +#pragma mark - Public methods + ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error +{ + XMLReader *reader = [[XMLReader alloc] initWithError:error]; + NSDictionary *rootDictionary = [reader objectWithData:data options:0]; + return rootDictionary; +} + ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error +{ + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; + return [XMLReader dictionaryForXMLData:data error:error]; +} + ++ (NSDictionary *)dictionaryForXMLData:(NSData *)data options:(XMLReaderOptions)options error:(NSError **)error +{ + XMLReader *reader = [[XMLReader alloc] initWithError:error]; + NSDictionary *rootDictionary = [reader objectWithData:data options:options]; + return rootDictionary; +} + ++ (NSDictionary *)dictionaryForXMLString:(NSString *)string options:(XMLReaderOptions)options error:(NSError **)error +{ + NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding]; + return [XMLReader dictionaryForXMLData:data options:options error:error]; +} + + +#pragma mark - Parsing + +- (id)initWithError:(NSError **)error +{ + self = [super init]; + if (self) + { + self.errorPointer = *error; + } + return self; +} + +- (NSDictionary *)objectWithData:(NSData *)data options:(XMLReaderOptions)options +{ + // Clear out any old data + self.dictionaryStack = [[NSMutableArray alloc] init]; + self.textInProgress = [[NSMutableString alloc] init]; + + // Initialize the stack with a fresh dictionary + [self.dictionaryStack addObject:[NSMutableDictionary dictionary]]; + + // Parse the XML + NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; + + [parser setShouldProcessNamespaces:(options & XMLReaderOptionsProcessNamespaces)]; + [parser setShouldReportNamespacePrefixes:(options & XMLReaderOptionsReportNamespacePrefixes)]; + [parser setShouldResolveExternalEntities:(options & XMLReaderOptionsResolveExternalEntities)]; + + parser.delegate = self; + BOOL success = [parser parse]; + + // Return the stack's root dictionary on success + if (success) + { + NSDictionary *resultDict = [self.dictionaryStack objectAtIndex:0]; + return resultDict; + } + + return nil; +} + + +#pragma mark - NSXMLParserDelegate methods + +- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict +{ + // Get the dictionary for the current level in the stack + NSMutableDictionary *parentDict = [self.dictionaryStack lastObject]; + + // Create the child dictionary for the new element, and initilaize it with the attributes + NSMutableDictionary *childDict = [NSMutableDictionary dictionary]; + [childDict addEntriesFromDictionary:attributeDict]; + + // If there's already an item for this key, it means we need to create an array + id existingValue = [parentDict objectForKey:elementName]; + if (existingValue) + { + NSMutableArray *array = nil; + if ([existingValue isKindOfClass:[NSMutableArray class]]) + { + // The array exists, so use it + array = (NSMutableArray *) existingValue; + } + else + { + // Create an array if it doesn't exist + array = [NSMutableArray array]; + [array addObject:existingValue]; + + // Replace the child dictionary with an array of children dictionaries + [parentDict setObject:array forKey:elementName]; + } + + // Add the new child dictionary to the array + [array addObject:childDict]; + } + else + { + // No existing value, so update the dictionary + [parentDict setObject:childDict forKey:elementName]; + } + + // Update the stack + [self.dictionaryStack addObject:childDict]; +} + +- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName +{ + // Update the parent dict with text info + NSMutableDictionary *dictInProgress = [self.dictionaryStack lastObject]; + + // Set the text property + if ([self.textInProgress length] > 0) + { + // trim after concatenating + NSString *trimmedString = [self.textInProgress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + [dictInProgress setObject:[trimmedString mutableCopy] forKey:kXMLReaderTextNodeKey]; + + // Reset the text + self.textInProgress = [[NSMutableString alloc] init]; + } + + // Pop the current dict + [self.dictionaryStack removeLastObject]; +} + +- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string +{ + // Build the text value + [self.textInProgress appendString:string]; +} + +- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError +{ + // Set the error pointer to the parser's error object + self.errorPointer = parseError; +} + +@end diff --git a/lib/authProviders.js b/lib/authProviders.js index bbde562..21601b1 100644 --- a/lib/authProviders.js +++ b/lib/authProviders.js @@ -26,6 +26,7 @@ export const authProviders = { request_token_url: 'https://api.twitter.com/oauth/request_token', authorize_url: 'https://api.twitter.com/oauth/authorize', access_token_url: 'https://api.twitter.com/oauth/access_token', + api_url: 'https://api.twitter.com/', callback_url: ({app_name}) => `${app_name}://oauth-response/twitter`, validate: validate({ @@ -36,6 +37,7 @@ export const authProviders = { 'facebook': { auth_version: "2.0", authorize_url: 'https://graph.facebook.com/oauth/authorize', + api_url: 'https://graph.facebook.com/', callback_url: ({client_id}) => `fb${client_id}://authorize`, validate: validate({ @@ -45,11 +47,44 @@ export const authProviders = { }, 'google': { auth_version: "2.0", - authorize_url: 'https://accounts.google.com/o/oauth2/v2/auth', + authorize_url: 'https://accounts.google.com/o/oauth2/auth', access_token_url: 'https://accounts.google.com/o/oauth2/token', - callback_url: ({app_name}) => `${app_name}:/oauth-response`, + callback_url: ({app_name}) => `${app_name}://oauth-response`, validate: validate() - } + }, + 'github': { + auth_version: '2.0', + authorize_url: 'https://github.com/login/oauth/authorize', + access_token_url: 'https://github.com/login/oauth/access_token', + api_url: 'https://api.github.com/', + callback_url: ({app_name}) => `${app_name}://oauth`, + validate: validate() + }, + 'slack': { + auth_version: '2.0', + authorize_url: 'https://slack.com/oauth/authorize', + access_token_url: 'https://slack.com/api/oauth.access', + api_url: 'https://slack.com/api', + callback_url: ({app_name}) => `${app_name}://oauth`, + defaultParams: { + token: 'access_token' + }, + validate: validate({ + client_id: [notEmpty], + client_secret: [notEmpty] + }) + }, + 'spotify': { + auth_version: "2.0", + authorize_url: 'https://accounts.spotify.com/authorize', + api_url: 'https://api.spotify.com/', + callback_url: ({app_name}) => `${app_name}://authorize`, + + validate: validate({ + client_id: [notEmpty], + client_secret: [notEmpty] + }) + }, } -export default authProviders; \ No newline at end of file +export default authProviders; diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..b3af007 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,801 @@ +{ + "name": "react-native-oauth", + "version": "2.1.18", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", + "dev": true + }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", + "dev": true + }, + "babel-code-frame": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", + "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", + "dev": true, + "requires": { + "chalk": "^1.1.3", + "esutils": "^2.0.2", + "js-tokens": "^3.0.2" + }, + "dependencies": { + "js-tokens": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", + "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", + "dev": true + } + } + }, + "babel-helper-builder-binary-assignment-operator-visitor": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-builder-binary-assignment-operator-visitor/-/babel-helper-builder-binary-assignment-operator-visitor-6.24.1.tgz", + "integrity": "sha1-zORReto1b0IgvK6KAsKzRvmlZmQ=", + "dev": true, + "requires": { + "babel-helper-explode-assignable-expression": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-helper-call-delegate": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-call-delegate/-/babel-helper-call-delegate-6.24.1.tgz", + "integrity": "sha1-7Oaqzdx25Bw0YfiL/Fdb0Nqi340=", + "dev": true, + "requires": { + "babel-helper-hoist-variables": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-helper-define-map": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-helper-define-map/-/babel-helper-define-map-6.26.0.tgz", + "integrity": "sha1-pfVtq0GiX5fstJjH66ypgZ+Vvl8=", + "dev": true, + "requires": { + "babel-helper-function-name": "^6.24.1", + "babel-runtime": "^6.26.0", + "babel-types": "^6.26.0", + "lodash": "^4.17.4" + } + }, + "babel-helper-explode-assignable-expression": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-explode-assignable-expression/-/babel-helper-explode-assignable-expression-6.24.1.tgz", + "integrity": "sha1-8luCz33BBDPFX3BZLVdGQArCLKo=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-helper-function-name": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-function-name/-/babel-helper-function-name-6.24.1.tgz", + "integrity": "sha1-00dbjAPtmCQqJbSDUasYOZ01gKk=", + "dev": true, + "requires": { + "babel-helper-get-function-arity": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-helper-get-function-arity": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-get-function-arity/-/babel-helper-get-function-arity-6.24.1.tgz", + "integrity": "sha1-j3eCqpNAfEHTqlCQj4mwMbG2hT0=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-helper-hoist-variables": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-hoist-variables/-/babel-helper-hoist-variables-6.24.1.tgz", + "integrity": "sha1-HssnaJydJVE+rbyZFKc/VAi+enY=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-helper-optimise-call-expression": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-optimise-call-expression/-/babel-helper-optimise-call-expression-6.24.1.tgz", + "integrity": "sha1-96E0J7qfc/j0+pk8VKl4gtEkQlc=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-helper-regex": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-helper-regex/-/babel-helper-regex-6.26.0.tgz", + "integrity": "sha1-MlxZ+QL4LyS3T6zu0DY5VPZJXnI=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "babel-types": "^6.26.0", + "lodash": "^4.17.4" + } + }, + "babel-helper-remap-async-to-generator": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-remap-async-to-generator/-/babel-helper-remap-async-to-generator-6.24.1.tgz", + "integrity": "sha1-XsWBgnrXI/7N04HxySg5BnbkVRs=", + "dev": true, + "requires": { + "babel-helper-function-name": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-helper-replace-supers": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-helper-replace-supers/-/babel-helper-replace-supers-6.24.1.tgz", + "integrity": "sha1-v22/5Dk40XNpohPKiov3S2qQqxo=", + "dev": true, + "requires": { + "babel-helper-optimise-call-expression": "^6.24.1", + "babel-messages": "^6.23.0", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-messages": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-messages/-/babel-messages-6.23.0.tgz", + "integrity": "sha1-8830cDhYA1sqKVHG7F7fbGLyYw4=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-check-es2015-constants": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-check-es2015-constants/-/babel-plugin-check-es2015-constants-6.22.0.tgz", + "integrity": "sha1-NRV7EBQm/S/9PaP3XH0ekYNbv4o=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-syntax-async-functions": { + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz", + "integrity": "sha1-ytnK0RkbWtY0vzCuCHI5HgZHvpU=", + "dev": true + }, + "babel-plugin-syntax-exponentiation-operator": { + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz", + "integrity": "sha1-nufoM3KQ2pUoggGmpX9BcDF4MN4=", + "dev": true + }, + "babel-plugin-syntax-object-rest-spread": { + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz", + "integrity": "sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=", + "dev": true + }, + "babel-plugin-syntax-trailing-function-commas": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-6.22.0.tgz", + "integrity": "sha1-ugNgk3+NBuQBgKQ/4NVhb/9TLPM=", + "dev": true + }, + "babel-plugin-transform-async-to-generator": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-async-to-generator/-/babel-plugin-transform-async-to-generator-6.24.1.tgz", + "integrity": "sha1-ZTbjeK/2yx1VF6wOQOs+n8jQh2E=", + "dev": true, + "requires": { + "babel-helper-remap-async-to-generator": "^6.24.1", + "babel-plugin-syntax-async-functions": "^6.8.0", + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-arrow-functions": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz", + "integrity": "sha1-RSaSy3EdX3ncf4XkQM5BufJE0iE=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-block-scoped-functions": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-block-scoped-functions/-/babel-plugin-transform-es2015-block-scoped-functions-6.22.0.tgz", + "integrity": "sha1-u8UbSflk1wy42OC5ToICRs46YUE=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-block-scoping": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-block-scoping/-/babel-plugin-transform-es2015-block-scoping-6.26.0.tgz", + "integrity": "sha1-1w9SmcEwjQXBL0Y4E7CgnnOxiV8=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "babel-template": "^6.26.0", + "babel-traverse": "^6.26.0", + "babel-types": "^6.26.0", + "lodash": "^4.17.4" + } + }, + "babel-plugin-transform-es2015-classes": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-classes/-/babel-plugin-transform-es2015-classes-6.24.1.tgz", + "integrity": "sha1-WkxYpQyclGHlZLSyo7+ryXolhNs=", + "dev": true, + "requires": { + "babel-helper-define-map": "^6.24.1", + "babel-helper-function-name": "^6.24.1", + "babel-helper-optimise-call-expression": "^6.24.1", + "babel-helper-replace-supers": "^6.24.1", + "babel-messages": "^6.23.0", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-computed-properties": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-computed-properties/-/babel-plugin-transform-es2015-computed-properties-6.24.1.tgz", + "integrity": "sha1-b+Ko0WiV1WNPTNmZttNICjCBWbM=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-destructuring": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-destructuring/-/babel-plugin-transform-es2015-destructuring-6.23.0.tgz", + "integrity": "sha1-mXux8auWf2gtKwh2/jWNYOdlxW0=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-duplicate-keys": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-duplicate-keys/-/babel-plugin-transform-es2015-duplicate-keys-6.24.1.tgz", + "integrity": "sha1-c+s9MQypaePvnskcU3QabxV2Qj4=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-for-of": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-for-of/-/babel-plugin-transform-es2015-for-of-6.23.0.tgz", + "integrity": "sha1-9HyVsrYT3x0+zC/bdXNiPHUkhpE=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-function-name": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-function-name/-/babel-plugin-transform-es2015-function-name-6.24.1.tgz", + "integrity": "sha1-g0yJhTvDaxrw86TF26qU/Y6sqos=", + "dev": true, + "requires": { + "babel-helper-function-name": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-literals": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-literals/-/babel-plugin-transform-es2015-literals-6.22.0.tgz", + "integrity": "sha1-T1SgLWzWbPkVKAAZox0xklN3yi4=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-modules-amd": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-modules-amd/-/babel-plugin-transform-es2015-modules-amd-6.24.1.tgz", + "integrity": "sha1-Oz5UAXI5hC1tGcMBHEvS8AoA0VQ=", + "dev": true, + "requires": { + "babel-plugin-transform-es2015-modules-commonjs": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-modules-commonjs": { + "version": "6.26.2", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-modules-commonjs/-/babel-plugin-transform-es2015-modules-commonjs-6.26.2.tgz", + "integrity": "sha512-CV9ROOHEdrjcwhIaJNBGMBCodN+1cfkwtM1SbUHmvyy35KGT7fohbpOxkE2uLz1o6odKK2Ck/tz47z+VqQfi9Q==", + "dev": true, + "requires": { + "babel-plugin-transform-strict-mode": "^6.24.1", + "babel-runtime": "^6.26.0", + "babel-template": "^6.26.0", + "babel-types": "^6.26.0" + } + }, + "babel-plugin-transform-es2015-modules-systemjs": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-modules-systemjs/-/babel-plugin-transform-es2015-modules-systemjs-6.24.1.tgz", + "integrity": "sha1-/4mhQrkRmpBhlfXxBuzzBdlAfSM=", + "dev": true, + "requires": { + "babel-helper-hoist-variables": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-modules-umd": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-modules-umd/-/babel-plugin-transform-es2015-modules-umd-6.24.1.tgz", + "integrity": "sha1-rJl+YoXNGO1hdq22B9YCNErThGg=", + "dev": true, + "requires": { + "babel-plugin-transform-es2015-modules-amd": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-object-super": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-object-super/-/babel-plugin-transform-es2015-object-super-6.24.1.tgz", + "integrity": "sha1-JM72muIcuDp/hgPa0CH1cusnj40=", + "dev": true, + "requires": { + "babel-helper-replace-supers": "^6.24.1", + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-parameters": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-parameters/-/babel-plugin-transform-es2015-parameters-6.24.1.tgz", + "integrity": "sha1-V6w1GrScrxSpfNE7CfZv3wpiXys=", + "dev": true, + "requires": { + "babel-helper-call-delegate": "^6.24.1", + "babel-helper-get-function-arity": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-template": "^6.24.1", + "babel-traverse": "^6.24.1", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-shorthand-properties": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-shorthand-properties/-/babel-plugin-transform-es2015-shorthand-properties-6.24.1.tgz", + "integrity": "sha1-JPh11nIch2YbvZmkYi5R8U3jiqA=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-spread": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-spread/-/babel-plugin-transform-es2015-spread-6.22.0.tgz", + "integrity": "sha1-1taKmfia7cRTbIGlQujdnxdG+NE=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-sticky-regex": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-sticky-regex/-/babel-plugin-transform-es2015-sticky-regex-6.24.1.tgz", + "integrity": "sha1-AMHNsaynERLN8M9hJsLta0V8zbw=", + "dev": true, + "requires": { + "babel-helper-regex": "^6.24.1", + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-plugin-transform-es2015-template-literals": { + "version": "6.22.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-template-literals/-/babel-plugin-transform-es2015-template-literals-6.22.0.tgz", + "integrity": "sha1-qEs0UPfp+PH2g51taH2oS7EjbY0=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-typeof-symbol": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-typeof-symbol/-/babel-plugin-transform-es2015-typeof-symbol-6.23.0.tgz", + "integrity": "sha1-3sCfHN3/lLUqxz1QXITfWdzOs3I=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-es2015-unicode-regex": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-unicode-regex/-/babel-plugin-transform-es2015-unicode-regex-6.24.1.tgz", + "integrity": "sha1-04sS9C6nMj9yk4fxinxa4frrNek=", + "dev": true, + "requires": { + "babel-helper-regex": "^6.24.1", + "babel-runtime": "^6.22.0", + "regexpu-core": "^2.0.0" + } + }, + "babel-plugin-transform-exponentiation-operator": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-exponentiation-operator/-/babel-plugin-transform-exponentiation-operator-6.24.1.tgz", + "integrity": "sha1-KrDJx/MJj6SJB3cruBP+QejeOg4=", + "dev": true, + "requires": { + "babel-helper-builder-binary-assignment-operator-visitor": "^6.24.1", + "babel-plugin-syntax-exponentiation-operator": "^6.8.0", + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-object-rest-spread": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz", + "integrity": "sha1-DzZpLVD+9rfi1LOsFHgTepY7ewY=", + "dev": true, + "requires": { + "babel-plugin-syntax-object-rest-spread": "^6.8.0", + "babel-runtime": "^6.26.0" + } + }, + "babel-plugin-transform-regenerator": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz", + "integrity": "sha1-4HA2lvveJ/Cj78rPi03KL3s6jy8=", + "dev": true, + "requires": { + "regenerator-transform": "^0.10.0" + } + }, + "babel-plugin-transform-runtime": { + "version": "6.23.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-runtime/-/babel-plugin-transform-runtime-6.23.0.tgz", + "integrity": "sha1-iEkNRGUC6puOfvsP4J7E2ZR5se4=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0" + } + }, + "babel-plugin-transform-strict-mode": { + "version": "6.24.1", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz", + "integrity": "sha1-1fr3qleKZbvlkc9e2uBKDGcCB1g=", + "dev": true, + "requires": { + "babel-runtime": "^6.22.0", + "babel-types": "^6.24.1" + } + }, + "babel-preset-env": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/babel-preset-env/-/babel-preset-env-1.7.0.tgz", + "integrity": "sha512-9OR2afuKDneX2/q2EurSftUYM0xGu4O2D9adAhVfADDhrYDaxXV0rBbevVYoY9n6nyX1PmQW/0jtpJvUNr9CHg==", + "dev": true, + "requires": { + "babel-plugin-check-es2015-constants": "^6.22.0", + "babel-plugin-syntax-trailing-function-commas": "^6.22.0", + "babel-plugin-transform-async-to-generator": "^6.22.0", + "babel-plugin-transform-es2015-arrow-functions": "^6.22.0", + "babel-plugin-transform-es2015-block-scoped-functions": "^6.22.0", + "babel-plugin-transform-es2015-block-scoping": "^6.23.0", + "babel-plugin-transform-es2015-classes": "^6.23.0", + "babel-plugin-transform-es2015-computed-properties": "^6.22.0", + "babel-plugin-transform-es2015-destructuring": "^6.23.0", + "babel-plugin-transform-es2015-duplicate-keys": "^6.22.0", + "babel-plugin-transform-es2015-for-of": "^6.23.0", + "babel-plugin-transform-es2015-function-name": "^6.22.0", + "babel-plugin-transform-es2015-literals": "^6.22.0", + "babel-plugin-transform-es2015-modules-amd": "^6.22.0", + "babel-plugin-transform-es2015-modules-commonjs": "^6.23.0", + "babel-plugin-transform-es2015-modules-systemjs": "^6.23.0", + "babel-plugin-transform-es2015-modules-umd": "^6.23.0", + "babel-plugin-transform-es2015-object-super": "^6.22.0", + "babel-plugin-transform-es2015-parameters": "^6.23.0", + "babel-plugin-transform-es2015-shorthand-properties": "^6.22.0", + "babel-plugin-transform-es2015-spread": "^6.22.0", + "babel-plugin-transform-es2015-sticky-regex": "^6.22.0", + "babel-plugin-transform-es2015-template-literals": "^6.22.0", + "babel-plugin-transform-es2015-typeof-symbol": "^6.23.0", + "babel-plugin-transform-es2015-unicode-regex": "^6.22.0", + "babel-plugin-transform-exponentiation-operator": "^6.22.0", + "babel-plugin-transform-regenerator": "^6.22.0", + "browserslist": "^3.2.6", + "invariant": "^2.2.2", + "semver": "^5.3.0" + } + }, + "babel-runtime": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-runtime/-/babel-runtime-6.26.0.tgz", + "integrity": "sha1-llxwWGaOgrVde/4E/yM3vItWR/4=", + "dev": true, + "requires": { + "core-js": "^2.4.0", + "regenerator-runtime": "^0.11.0" + } + }, + "babel-template": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-template/-/babel-template-6.26.0.tgz", + "integrity": "sha1-3gPi0WOWsGn0bdn/+FIfsaDjXgI=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "babel-traverse": "^6.26.0", + "babel-types": "^6.26.0", + "babylon": "^6.18.0", + "lodash": "^4.17.4" + } + }, + "babel-traverse": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-traverse/-/babel-traverse-6.26.0.tgz", + "integrity": "sha1-RqnL1+3MYsjlwGTi0tjQ9ANXZu4=", + "dev": true, + "requires": { + "babel-code-frame": "^6.26.0", + "babel-messages": "^6.23.0", + "babel-runtime": "^6.26.0", + "babel-types": "^6.26.0", + "babylon": "^6.18.0", + "debug": "^2.6.8", + "globals": "^9.18.0", + "invariant": "^2.2.2", + "lodash": "^4.17.4" + } + }, + "babel-types": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-types/-/babel-types-6.26.0.tgz", + "integrity": "sha1-o7Bz+Uq0nrb6Vc1lInozQ4BjJJc=", + "dev": true, + "requires": { + "babel-runtime": "^6.26.0", + "esutils": "^2.0.2", + "lodash": "^4.17.4", + "to-fast-properties": "^1.0.3" + } + }, + "babylon": { + "version": "6.18.0", + "resolved": "https://registry.npmjs.org/babylon/-/babylon-6.18.0.tgz", + "integrity": "sha512-q/UEjfGJ2Cm3oKV71DJz9d25TPnq5rhBVL2Q4fA5wcC3jcrdn7+SssEybFIxwAvvP+YCsCYNKughoF33GxgycQ==", + "dev": true + }, + "browserslist": { + "version": "3.2.8", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-3.2.8.tgz", + "integrity": "sha512-WHVocJYavUwVgVViC0ORikPHQquXwVh939TaelZ4WDqpWgTX/FsGhl/+P4qBUAGcRvtOgDgC+xftNWWp2RUTAQ==", + "dev": true, + "requires": { + "caniuse-lite": "^1.0.30000844", + "electron-to-chromium": "^1.3.47" + } + }, + "caniuse-lite": { + "version": "1.0.30000865", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000865.tgz", + "integrity": "sha512-vs79o1mOSKRGv/1pSkp4EXgl4ZviWeYReXw60XfacPU64uQWZwJT6vZNmxRF9O+6zu71sJwMxLK5JXxbzuVrLw==", + "dev": true + }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "dev": true, + "requires": { + "ansi-styles": "^2.2.1", + "escape-string-regexp": "^1.0.2", + "has-ansi": "^2.0.0", + "strip-ansi": "^3.0.0", + "supports-color": "^2.0.0" + } + }, + "core-js": { + "version": "2.5.7", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.7.tgz", + "integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw==", + "dev": true + }, + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "electron-to-chromium": { + "version": "1.3.52", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.52.tgz", + "integrity": "sha1-0tnxJwuko7lnuDHEDvcftNmrXOA=", + "dev": true + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", + "dev": true + }, + "esutils": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", + "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", + "dev": true + }, + "globals": { + "version": "9.18.0", + "resolved": "https://registry.npmjs.org/globals/-/globals-9.18.0.tgz", + "integrity": "sha512-S0nG3CLEQiY/ILxqtztTWH/3iRRdyBLw6KMDxnKMchrtbj2OFmehVh0WUCfW3DUrIgx/qFrJPICrq4Z4sTR9UQ==", + "dev": true + }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "invariant": { + "version": "2.2.4", + "resolved": "https://registry.npmjs.org/invariant/-/invariant-2.2.4.tgz", + "integrity": "sha512-phJfQVBuaJM5raOpJjSfkiD6BpbCE4Ns//LaXl6wGYtUBY83nWS6Rf9tXm2e8VaK60JEjYldbPif/A2B1C2gNA==", + "requires": { + "loose-envify": "^1.0.0" + } + }, + "js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + }, + "jsesc": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-0.5.0.tgz", + "integrity": "sha1-597mbjXW/Bb3EP6R1c9p9w8IkR0=", + "dev": true + }, + "lodash": { + "version": "4.17.10", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.10.tgz", + "integrity": "sha512-UejweD1pDoXu+AD825lWwp4ZGtSwgnpZxb3JDViD7StjQz+Nb/6l093lx4OQ0foGWNRoc19mWy7BzL+UAK2iVg==", + "dev": true + }, + "loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "requires": { + "js-tokens": "^3.0.0 || ^4.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + }, + "private": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/private/-/private-0.1.8.tgz", + "integrity": "sha512-VvivMrbvd2nKkiG38qjULzlc+4Vx4wm/whI9pQD35YrARNnhxeiRktSOhSukRLFNlzg6Br/cJPet5J/u19r/mg==", + "dev": true + }, + "regenerate": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/regenerate/-/regenerate-1.4.0.tgz", + "integrity": "sha512-1G6jJVDWrt0rK99kBjvEtziZNCICAuvIPkSiUFIQxVP06RCVpq3dmDo2oi6ABpYaDYaTRr67BEhL8r1wgEZZKg==", + "dev": true + }, + "regenerator-runtime": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.11.1.tgz", + "integrity": "sha512-MguG95oij0fC3QV3URf4V2SDYGJhJnJGqvIIgdECeODCT98wSWDAJ94SSuVpYQUoTcGUIL6L4yNB7j1DFFHSBg==", + "dev": true + }, + "regenerator-transform": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/regenerator-transform/-/regenerator-transform-0.10.1.tgz", + "integrity": "sha512-PJepbvDbuK1xgIgnau7Y90cwaAmO/LCLMI2mPvaXq2heGMR3aWW5/BQvYrhJ8jgmQjXewXvBjzfqKcVOmhjZ6Q==", + "dev": true, + "requires": { + "babel-runtime": "^6.18.0", + "babel-types": "^6.19.0", + "private": "^0.1.6" + } + }, + "regexpu-core": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/regexpu-core/-/regexpu-core-2.0.0.tgz", + "integrity": "sha1-SdA4g3uNz4v6W5pCE5k45uoq4kA=", + "dev": true, + "requires": { + "regenerate": "^1.2.1", + "regjsgen": "^0.2.0", + "regjsparser": "^0.1.4" + } + }, + "regjsgen": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/regjsgen/-/regjsgen-0.2.0.tgz", + "integrity": "sha1-bAFq3qxVT3WCP+N6wFuS1aTtsfc=", + "dev": true + }, + "regjsparser": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/regjsparser/-/regjsparser-0.1.5.tgz", + "integrity": "sha1-fuj4Tcb6eS0/0K4ijSS9lJ6tIFw=", + "dev": true, + "requires": { + "jsesc": "~0.5.0" + } + }, + "semver": { + "version": "5.5.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz", + "integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==", + "dev": true + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "dev": true, + "requires": { + "ansi-regex": "^2.0.0" + } + }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", + "dev": true + }, + "to-fast-properties": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-1.0.3.tgz", + "integrity": "sha1-uDVx+k2MJbguIxsG46MFXeTKGkc=", + "dev": true + }, + "valib": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/valib/-/valib-2.0.0.tgz", + "integrity": "sha1-4NRVsQ4XrmBcDzOXGtrHNWTuGKc=" + } + } +} diff --git a/package.json b/package.json index 5ed3130..453f8c4 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "react-native-oauth", - "version": "1.0.7", + "version": "2.1.18", "author": "Ari Lerner (https://fullstackreact.com)", "description": "An oauth manager for dealing with the complexities of oauth", "main": "./react-native-oauth.js", "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", - "build": "./node_modules/.bin/babel --source-maps=true --out-dir=dist .", + "build": "./node_modules/.bin/babel --ignore 'node_modules,dist' --source-maps=true --out-dir=dist .", "dev": "npm run compile -- --watch", "lint": "eslint ./src", "publish_pages": "gh-pages -d public/", @@ -39,11 +39,18 @@ "project": "ios/OAuthManager.xcodeproj" }, "android": { - "packageInstance": "new OAuthManagerPackage(getApplicationContext())" + "packageInstance": "new OAuthManagerPackage()" } }, "dependencies": { "invariant": "^2.2.1", "valib": "^2.0.0" + }, + "devDependencies": { + "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-plugin-transform-object-rest-spread": "^6.26.0", + "babel-plugin-transform-runtime": "^6.23.0", + "babel-preset-env": "^1.7.0", + "babel-runtime": "^6.26.0" } } diff --git a/react-native-oauth.js b/react-native-oauth.js index ebbaac4..3008cf0 100644 --- a/react-native-oauth.js +++ b/react-native-oauth.js @@ -13,7 +13,9 @@ const OAuthManagerBridge = NativeModules.OAuthManager; let configured = false; const STORAGE_KEY = 'ReactNativeOAuth'; import promisify from './lib/promisify' -import authProviders from './lib/authProviders'; +import defaultProviders from './lib/authProviders'; + +let authProviders = defaultProviders; const identity = (props) => props; /** @@ -27,6 +29,10 @@ export default class OAuthManager { this._options = opts; } + addProvider(provider) { + authProviders = Object.assign({}, authProviders, provider); + } + configure(providerConfigs) { return this.configureProviders(providerConfigs) } @@ -38,17 +44,50 @@ export default class OAuthManager { return promisify('authorize')(provider, options); } - makeRequest(provider, method, url, parameters={}, headers={}) { - return promisify(OAuthManagerBridge.makeSignedRequest)( - provider, method, url, parameters, headers); + savedAccounts(opts={}) { + // const options = Object.assign({}, this._options, opts, { + // app_name: this.appName + // }) + // return promisify('getSavedAccounts')(options); + const promises = this.providers() + .map(name => { + return this.savedAccount(name) + .catch(err => ({provider: name, status: "error"})); + }); + return Promise.all(promises) + .then((accountResp) => { + const accounts = accountResp.filter(acc => acc.status == "ok"); + return { accounts } + }); } - deauthorize(providerName) { - return new Promise((resolve, reject) => { - AsyncStorage.removeItem(this.makeStorageKey(providerName), (err) => { - return err ? reject(err) : resolve(); - }) + savedAccount(provider) { + const options = Object.assign({}, this._options, { + app_name: this.appName }) + return promisify('getSavedAccount')(provider, options); + } + + makeRequest(provider, url, opts={}) { + const options = Object.assign({}, this._options, opts, { + app_name: this.appName + }); + + console.log('making request', provider, url, opts); + + return promisify('makeRequest')(provider, url, options) + .then(response => { + // Little bit of a hack to support Android until we have a better + // way of decoding the JSON response on the Android side + if (response && response.data && typeof response.data === "string") { + response.data = JSON.parse(response.data); + } + return response; + }); + } + + deauthorize(provider) { + return promisify('deauthorize')(provider); } providers() { @@ -63,18 +102,14 @@ export default class OAuthManager { return OAuthManager.providers().indexOf(name) >= 0; } - makeStorageKey(path, prefix='credentials') { - return `${STORAGE_KEY}/${prefix}/${path}`.toLowerCase(); - } - // Private /** * Configure a single provider - * - * + * + * * @param {string} name of the provider * @param {object} additional configuration - * + * **/ configureProvider(name, props) { invariant(OAuthManager.isSupported(name), `The provider ${name} is not supported yet`); @@ -89,13 +124,18 @@ export default class OAuthManager { callback_url }, providerCfg, props); + if (config.defaultParams) { + delete config.defaultParams; + } + config = Object.keys(config) .reduce((sum, key) => ({ ...sum, [key]: typeof config[key] === 'function' ? config[key](config) : config[key] - }), {}) + }), {}); validate(config); + return promisify('configureProvider')(name, config); } diff --git a/resources/capabilities.png b/resources/capabilities.png new file mode 100644 index 0000000..9dfca7c Binary files /dev/null and b/resources/capabilities.png differ diff --git a/resources/facebook/app.png b/resources/facebook/app.png new file mode 100644 index 0000000..0a92a87 Binary files /dev/null and b/resources/facebook/app.png differ diff --git a/resources/facebook/dev.facebook.png b/resources/facebook/dev.facebook.png new file mode 100644 index 0000000..01acddd Binary files /dev/null and b/resources/facebook/dev.facebook.png differ diff --git a/resources/facebook/facebook-redirect.png b/resources/facebook/facebook-redirect.png new file mode 100644 index 0000000..57a6cd0 Binary files /dev/null and b/resources/facebook/facebook-redirect.png differ diff --git a/resources/facebook/redirect-url.png b/resources/facebook/redirect-url.png new file mode 100644 index 0000000..e032379 Binary files /dev/null and b/resources/facebook/redirect-url.png differ diff --git a/resources/facebook/url-scheme.png b/resources/facebook/url-scheme.png new file mode 100644 index 0000000..9123cf6 Binary files /dev/null and b/resources/facebook/url-scheme.png differ diff --git a/resources/github/apps.png b/resources/github/apps.png new file mode 100644 index 0000000..7920910 Binary files /dev/null and b/resources/github/apps.png differ diff --git a/resources/google/android-creds.png b/resources/google/android-creds.png new file mode 100644 index 0000000..4da2ef2 Binary files /dev/null and b/resources/google/android-creds.png differ diff --git a/resources/google/auth-page.png b/resources/google/auth-page.png new file mode 100644 index 0000000..b4b47d4 Binary files /dev/null and b/resources/google/auth-page.png differ diff --git a/resources/google/creds.png b/resources/google/creds.png new file mode 100644 index 0000000..e089e5a Binary files /dev/null and b/resources/google/creds.png differ diff --git a/resources/google/url-scheme.png b/resources/google/url-scheme.png new file mode 100644 index 0000000..864e25e Binary files /dev/null and b/resources/google/url-scheme.png differ diff --git a/resources/header-search-paths.png b/resources/header-search-paths.png new file mode 100644 index 0000000..7edbd3e Binary files /dev/null and b/resources/header-search-paths.png differ diff --git a/resources/info-panel.png b/resources/info-panel.png new file mode 100644 index 0000000..49824f8 Binary files /dev/null and b/resources/info-panel.png differ diff --git a/resources/slack/create.png b/resources/slack/create.png new file mode 100644 index 0000000..46c4fc5 Binary files /dev/null and b/resources/slack/create.png differ diff --git a/resources/slack/creds.png b/resources/slack/creds.png new file mode 100644 index 0000000..4126720 Binary files /dev/null and b/resources/slack/creds.png differ diff --git a/resources/slack/dev.png b/resources/slack/dev.png new file mode 100644 index 0000000..63f1120 Binary files /dev/null and b/resources/slack/dev.png differ diff --git a/resources/slack/getting_started.png b/resources/slack/getting_started.png new file mode 100644 index 0000000..fc2b880 Binary files /dev/null and b/resources/slack/getting_started.png differ diff --git a/resources/slack/redirect.png b/resources/slack/redirect.png new file mode 100644 index 0000000..86e40ae Binary files /dev/null and b/resources/slack/redirect.png differ diff --git a/resources/twitter/api-key.png b/resources/twitter/api-key.png new file mode 100644 index 0000000..6906c62 Binary files /dev/null and b/resources/twitter/api-key.png differ diff --git a/resources/twitter/app.png b/resources/twitter/app.png new file mode 100644 index 0000000..0fc7cd5 Binary files /dev/null and b/resources/twitter/app.png differ diff --git a/resources/twitter/callback-url.png b/resources/twitter/callback-url.png new file mode 100644 index 0000000..8ac2d7b Binary files /dev/null and b/resources/twitter/callback-url.png differ diff --git a/resources/twitter/url-scheme.png b/resources/twitter/url-scheme.png new file mode 100644 index 0000000..88f9689 Binary files /dev/null and b/resources/twitter/url-scheme.png differ diff --git a/resources/url-schemes.png b/resources/url-schemes.png new file mode 100644 index 0000000..1652215 Binary files /dev/null and b/resources/url-schemes.png differ