|
1 |
| -# Getting Started |
| 1 | +## 1. Create and configure an authenticator |
2 | 2 |
|
3 |
| -## JSch - Java/Android SSH2 library |
| 3 | +Let's authenticate a user to access Facebook: |
4 | 4 |
|
5 |
| -JSch for Xamarin.Android is component with Xamarin.Android bindings for JSch - SSH |
6 |
| -implementation in Java by JCraft. |
| 5 | +```csharp |
| 6 | +using Xamarin.Auth; |
| 7 | +... |
| 8 | +var auth = new OAuth2Authenticator ( |
| 9 | + clientId: "App ID from https://developers.facebook.com/apps", |
| 10 | + scope: "", |
| 11 | + authorizeUrl: new Uri ("https://m.facebook.com/dialog/oauth/"), |
| 12 | + redirectUrl: new Uri ("http://www.facebook.com/connect/login_success.html")); |
| 13 | +``` |
7 | 14 |
|
8 |
| -JSch is a pure Java implementation of SSH2. |
9 |
| -JSch allows users to |
| 15 | +Facebook uses OAuth 2.0 authentication, so we create an `OAuth2Authenticator`. |
| 16 | +Authenticators are responsible for managing the user interface and communicating with |
| 17 | +authentication services. |
10 | 18 |
|
11 |
| - * connect to an sshd server and use |
12 |
| - * port forwarding, |
13 |
| - * X11 forwarding, |
14 |
| - * file transfer, etc. |
| 19 | +Authenticators take a variety of parameters; in this case, the application's ID, its |
| 20 | +authorization scope, and Facebook's various service locations are required. |
15 | 21 |
|
16 |
| -Developers can integrate its functionality into Java (and Android of course) programs |
17 | 22 |
|
18 | 23 |
|
19 |
| -## Links/References |
20 | 24 |
|
| 25 | +## 2. Authenticate the user |
21 | 26 |
|
22 |
| -More about the library: |
| 27 | +While authenticators manage their own UI, it's up to you to initially present the |
| 28 | +authenticator's UI on the screen. This lets you control how the authentication UI is |
| 29 | +displayed–modally, in navigation controllers, in popovers, etc. |
23 | 30 |
|
24 |
| -* [http://www.jcraft.com/jsch/](http://www.jcraft.com/jsch/) |
| 31 | +Before we present the UI, we need to start listening to the `Completed` event which fires |
| 32 | +when the user successfully authenticates or cancels. You can find out if the authentication |
| 33 | +succeeded by testing the `IsAuthenticated` property of `eventArgs`: |
25 | 34 |
|
26 |
| -The Java (Android) sample that served as base for Xamarin.Android sample: |
| 35 | +```csharp |
| 36 | +auth.Completed += (sender, eventArgs) => { |
| 37 | + // We presented the UI, so it's up to us to dimiss it on iOS. |
| 38 | + DismissViewController (true, null); |
27 | 39 |
|
28 |
| -* [http://kennethsfrequency.com/2013/03/19/android-ssh/](http://kennethsfrequency.com/2013/03/19/android-ssh/) |
| 40 | + if (eventArgs.IsAuthenticated) { |
| 41 | + // Use eventArgs.Account to do wonderful things |
| 42 | + } else { |
| 43 | + // The user cancelled |
| 44 | + } |
| 45 | +}; |
| 46 | +``` |
29 | 47 |
|
| 48 | +All the information gathered from a successful authentication is available in |
| 49 | +`eventArgs.Account`. |
30 | 50 |
|
| 51 | +Now we're ready to present the login UI from `ViewDidAppear` on iOS: |
31 | 52 |
|
| 53 | +```csharp |
| 54 | +PresentViewController (auth.GetUI (), true, null); |
| 55 | +``` |
| 56 | + |
| 57 | +The `GetUI` method returns `UINavigationControllers` on iOS, and `Intents` on Android. |
| 58 | +On Android, we would write the following code to present the UI from `OnCreate`: |
| 59 | + |
| 60 | +```csharp |
| 61 | +StartActivity (auth.GetUI (this)); |
| 62 | +``` |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## 3. Making requests |
| 67 | + |
| 68 | +Since Facebook is an OAuth2 service, we'll make requests with `OAuth2Request` providing |
| 69 | +the account we retrieved from the `Completed` event. Assuming we're authenticated, we'll |
| 70 | +grab the user's info to demonstrate: |
| 71 | + |
| 72 | +```csharp |
| 73 | +var request = new OAuth2Request ("GET", new Uri ("https://graph.facebook.com/me"), null, eventArgs.Account); |
| 74 | +request.GetResponseAsync().ContinueWith (t => { |
| 75 | + if (t.IsFaulted) |
| 76 | + Console.WriteLine ("Error: " + t.Exception.InnerException.Message); |
| 77 | + else { |
| 78 | + string json = t.Result.GetResponseText(); |
| 79 | + Console.WriteLine (json); |
| 80 | + } |
| 81 | +}); |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | +## 4. Store the account |
| 86 | + |
| 87 | +Xamarin.Auth securely stores `Account` objects so that you don't always have to reauthenticate |
| 88 | +the user. The `AccountStore` class is responsible for storing `Account` information, backed by |
| 89 | +the [Keychain](https://developer.apple.com/library/ios/#documentation/security/Reference/keychainservices/Reference/reference.html) |
| 90 | +on iOS and a [KeyStore](http://developer.android.com/reference/java/security/KeyStore.html) on |
| 91 | +Android: |
| 92 | + |
| 93 | +```csharp |
| 94 | +// On iOS: |
| 95 | +AccountStore.Create ().Save (eventArgs.Account, "Facebook"); |
| 96 | + |
| 97 | +// On Android: |
| 98 | +AccountStore.Create (this).Save (eventArgs.Account, "Facebook"); |
| 99 | +``` |
| 100 | + |
| 101 | +Saved Accounts are uniquely identified using a key composed of the account's |
| 102 | +`Username` property and a "Service ID". The "Service ID" is any string that is |
| 103 | +used when fetching accounts from the store. |
| 104 | + |
| 105 | +If an `Account` was previously saved, calling `Save` again will overwrite it. |
| 106 | +This is convenient for services that expire the credentials stored in the account |
| 107 | +object. |
| 108 | + |
| 109 | + |
| 110 | +## 5. Retrieve stored accounts |
| 111 | + |
| 112 | +You can fetch all `Account` objects stored for a given service: |
| 113 | + |
| 114 | +```csharp |
| 115 | +// On iOS: |
| 116 | +IEnumerable<Account> accounts = AccountStore.Create ().FindAccountsForService ("Facebook"); |
| 117 | + |
| 118 | +// On Android: |
| 119 | +IEnumerable<Account> accounts = AccountStore.Create (this).FindAccountsForService ("Facebook"); |
| 120 | +``` |
| 121 | + |
| 122 | +It's that easy. |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## 6. Make your own authenticator |
| 128 | + |
| 129 | +Xamarin.Auth includes OAuth 1.0 and OAuth 2.0 authenticators, providing support for thousands |
| 130 | +of popular services. For services that use traditional username/password authentication, you |
| 131 | +can roll your own authenticator by deriving from `FormAuthenticator`. |
| 132 | + |
| 133 | +If you want to authenticate against an ostensibly unsupported service, fear not – Xamarin.Auth |
| 134 | +is extensible! It's very easy to create your own authenticators – just derive from any of the |
| 135 | +existing authenticators and start overriding methods. |
32 | 136 |
|
0 commit comments