<commit_before># KTOAuth

KTOAuth is a Swift library which let's the user authenticate against a webservice using OAuth2. I'm using to authenticate through a `UIWebView` inside a `UIViewController`, but you could probably still use it in Safari as well (hint: use `openURL:` and handle the redirect in your app).

## Implementation

In order for you to receive an `access_token`, you'll have to instantiate a new instance of `KTOAuthClient` and set the `UIViewController` as the delegate of the client:

```
var client = KTOAuthClient(clientId: Config.OAuth.ClientID, clientSecret: Config.OAuth.ClientSecret, redirectURI: Config.OAuth.RedirectURI, authorizeURL: Config.OAuth.AuthorizeURL, tokenURL: Config.OAuth.TokenURL)
client.delegate = self
```

I've used the example `Config` struct included in this repository to set the required data and passed that into the `KTOAuthClient` init as can be seen above.

The next step is to generate the authorize URL which you'll have to load in the `UIWebView`:

```
webView.loadRequest(NSURLRequest(URL: NSURL(string: client.authorizeUrl())!))
```

Next up is the call to the `retrieveAuthorizationCode` method to start the OAuth2 process. You'll probably want to call it in the `webView:shouldStartLoadWithRequest:navigationType` delegate method of `UIWebViewDelegate`:

```
extension WebViewController: UIWebViewDelegate {
  func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    return client.retrieveAuthorizationCode(request.URL.absoluteString!)
  }
}
```

Now all you need to do is add the required `KTOAuthClientDelegate` protocol methods and you're done:

```
extension WebViewController: KTOAuthClientDelegate {
  func didReceiveAccessToken(json: JSON) {
  	println(json)
  }

  func didReceiveAccessTokenError(error: NSError) -> Void {
    println(error)
  }

  func didReceiveAuthorizationCodeError(error: NSError) -> Void {
    println(error)
  }
}
```

## Dependencies

* [Alamofire](https://github.com/Alamofire/Alamofire) - Elegant HTTP Networking in Swift.
* [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) - The better way to deal with JSON data in Swift.

## Copyright

Copyright 2015 <NAME>. Released under the MIT License.<commit_msg>Explicitly set Swift as the language used in the code examples.<commit_after># KTOAuth

KTOAuth is a Swift library which let's the user authenticate against a webservice using OAuth2. I'm using to authenticate through a `UIWebView` inside a `UIViewController`, but you could probably still use it in Safari as well (hint: use `openURL:` and handle the redirect in your app).

## Usage

In order for you to receive an `access_token`, you'll have to instantiate a new instance of `KTOAuthClient` and set the `UIViewController` as the delegate of the client:

```swift
var client = KTOAuthClient(clientId: Config.OAuth.ClientID, clientSecret: Config.OAuth.ClientSecret, redirectURI: Config.OAuth.RedirectURI, authorizeURL: Config.OAuth.AuthorizeURL, tokenURL: Config.OAuth.TokenURL)
client.delegate = self
```

I've used the example `Config` struct included in this repository to set the required data and passed that into the `KTOAuthClient` init as can be seen above.

The next step is to generate the authorize URL which you'll have to load in the `UIWebView`:

```swift
webView.loadRequest(NSURLRequest(URL: NSURL(string: client.authorizeUrl())!))
```

Next up is the call to the `retrieveAuthorizationCode` method to start the OAuth2 process. You'll probably want to call it in the `webView:shouldStartLoadWithRequest:navigationType` delegate method of `UIWebViewDelegate`:

```swift
extension WebViewController: UIWebViewDelegate {
  func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    return client.retrieveAuthorizationCode(request.URL.absoluteString!)
  }
}
```

Now all you need to do is add the required `KTOAuthClientDelegate` protocol methods and you're done:

```swift
extension WebViewController: KTOAuthClientDelegate {
  func didReceiveAccessToken(json: JSON) {
  	println(json)
  }

  func didReceiveAccessTokenError(error: NSError) -> Void {
    println(error)
  }

  func didReceiveAuthorizationCodeError(error: NSError) -> Void {
    println(error)
  }
}
```

## Dependencies

* [Alamofire](https://github.com/Alamofire/Alamofire) - Elegant HTTP Networking in Swift.
* [SwiftyJSON](https://github.com/SwiftyJSON/SwiftyJSON) - The better way to deal with JSON data in Swift.

## Copyright

Copyright 2015 <NAME>. Released under the MIT License.
{'fraction_non_alphanumeric': 0.08647832650420531, 'fraction_numerical': 0.002587880094888937, 'mean_word_length': 5.406077348066298, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1102258', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1307, 'n_words': 489}