Skip to content

Commit 5adf97c

Browse files
authored
Merge pull request #49 from auth0/feature-swift-3
Swift 3
2 parents ce36a98 + 47d3af6 commit 5adf97c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+944
-1052
lines changed

.swift-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3
1+
3.0

App/AppDelegate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2828

2929
var window: UIWindow?
3030

31-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
31+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
3232
return true
3333
}
3434

35-
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
35+
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
3636
return Auth0.resumeAuth(url, options: options)
3737
}
3838
}

App/ViewController.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,39 @@ import Auth0
1111

1212
class ViewController: UIViewController {
1313

14-
var onAuth: (Result<Credentials> -> ())!
14+
var onAuth: ((Result<Credentials>) -> ())!
1515

1616
@IBOutlet weak var oauth2: UIButton!
1717

1818
override func viewDidLoad() {
1919
super.viewDidLoad()
2020
self.onAuth = { [weak self] in
2121
switch $0 {
22-
case .Failure(let cause):
23-
let alert = UIAlertController(title: "Auth Failed!", message: "\(cause)", preferredStyle: .Alert)
24-
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
25-
self?.presentViewController(alert, animated: true, completion: nil)
26-
case .Success(let credentials):
27-
let alert = UIAlertController(title: "Auth Success!", message: "Authorized and got access_token \(credentials.accessToken)", preferredStyle: .Alert)
28-
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
29-
self?.presentViewController(alert, animated: true, completion: nil)
22+
case .failure(let cause):
23+
let alert = UIAlertController(title: "Auth Failed!", message: "\(cause)", preferredStyle: .alert)
24+
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
25+
self?.present(alert, animated: true, completion: nil)
26+
case .success(let credentials):
27+
let alert = UIAlertController(title: "Auth Success!", message: "Authorized and got access_token \(credentials.accessToken)", preferredStyle: .alert)
28+
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
29+
self?.present(alert, animated: true, completion: nil)
3030
}
3131
print($0)
3232
}
3333
}
3434

35-
@IBAction func startOAuth2(sender: AnyObject) {
35+
@IBAction func startOAuth2(_ sender: Any) {
3636
var auth0 = Auth0.webAuth()
3737
auth0
3838
.logging(enabled: true)
3939
.start(onAuth)
4040
}
4141

42-
@IBAction func startGoogleOAuth2(sender: AnyObject) {
42+
@IBAction func startGoogleOAuth2(_ sender: Any) {
4343
var auth0 = Auth0.webAuth()
4444
auth0
4545
.logging(enabled: true)
4646
.connection("google-oauth2")
4747
.start(onAuth)
4848
}
49-
}
49+
}

Auth0.xcodeproj/project.pbxproj

+26-30
Large diffs are not rendered by default.

Auth0/Auth0.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Foundation
3535

3636
- returns: Auth0 Authentication API
3737
*/
38-
public func authentication(clientId clientId: String, domain: String, session: NSURLSession = .sharedSession()) -> Authentication {
38+
public func authentication(clientId: String, domain: String, session: URLSession = .shared) -> Authentication {
3939
return Auth0Authentication(clientId: clientId, url: .a0_url(domain), session: session)
4040
}
4141

@@ -67,7 +67,7 @@ public func authentication(clientId clientId: String, domain: String, session: N
6767
- returns: Auth0 Authentication API
6868
- important: Calling this method without a valid `Auth0.plist` will crash your application
6969
*/
70-
public func authentication(session session: NSURLSession = .sharedSession(), bundle: NSBundle = NSBundle.mainBundle()) -> Authentication {
70+
public func authentication(session: URLSession = .shared, bundle: Bundle = .main) -> Authentication {
7171
let values = plistValues(bundle: bundle)!
7272
return authentication(clientId: values.clientId, domain: values.domain, session: session)
7373
}
@@ -108,7 +108,7 @@ public func authentication(session session: NSURLSession = .sharedSession(), bun
108108
- returns: Auth0 Management API v2
109109
- important: Calling this method without a valid `Auth0.plist` will crash your application
110110
*/
111-
public func users(token token: String, session: NSURLSession = .sharedSession(), bundle: NSBundle = NSBundle.mainBundle()) -> Users {
111+
public func users(token: String, session: URLSession = .shared, bundle: Bundle = .main) -> Users {
112112
let values = plistValues(bundle: bundle)!
113113
return users(token: token, domain: values.domain, session: session)
114114
}
@@ -133,14 +133,14 @@ public func users(token token: String, session: NSURLSession = .sharedSession(),
133133

134134
- returns: Auth0 Management API v2
135135
*/
136-
public func users(token token: String, domain: String, session: NSURLSession = .sharedSession()) -> Users {
136+
public func users(token: String, domain: String, session: URLSession = .shared) -> Users {
137137
return Management(token: token, url: .a0_url(domain), session: session)
138138
}
139139

140-
func plistValues(bundle bundle: NSBundle) -> (clientId: String, domain: String)? {
140+
func plistValues(bundle: Bundle) -> (clientId: String, domain: String)? {
141141
guard
142-
let path = bundle.pathForResource("Auth0", ofType: "plist"),
143-
let values = NSDictionary(contentsOfFile: path) as? [String: AnyObject]
142+
let path = bundle.path(forResource: "Auth0", ofType: "plist"),
143+
let values = NSDictionary(contentsOfFile: path) as? [String: Any]
144144
else {
145145
print("Missing Auth0.plist file with 'ClientId' and 'Domain' entries in main bundle!")
146146
return nil

Auth0/Auth0Authentication.swift

+32-32
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ import Foundation
2424

2525
struct Auth0Authentication: Authentication {
2626
let clientId: String
27-
let url: NSURL
27+
let url: URL
2828
var telemetry: Telemetry
2929
var logger: Logger?
3030

31-
let session: NSURLSession
31+
let session: URLSession
3232

33-
init(clientId: String, url: NSURL, session: NSURLSession = NSURLSession.sharedSession(), telemetry: Telemetry = Telemetry()) {
33+
init(clientId: String, url: URL, session: URLSession = URLSession.shared, telemetry: Telemetry = Telemetry()) {
3434
self.clientId = clientId
3535
self.url = url
3636
self.session = session
3737
self.telemetry = telemetry
3838
}
3939

40-
func login(usernameOrEmail username: String, password: String, multifactorCode: String?, connection: String, scope: String, parameters: [String: AnyObject]) -> Request<Credentials, AuthenticationError> {
41-
let resourceOwner = NSURL(string: "/oauth/ro", relativeToURL: self.url)!
42-
var payload: [String: AnyObject] = [
40+
func login(usernameOrEmail username: String, password: String, multifactorCode: String?, connection: String, scope: String, parameters: [String: Any]) -> Request<Credentials, AuthenticationError> {
41+
let resourceOwner = URL(string: "/oauth/ro", relativeTo: self.url)!
42+
var payload: [String: Any] = [
4343
"username": username,
4444
"password": password,
4545
"connection": connection,
@@ -53,8 +53,8 @@ struct Auth0Authentication: Authentication {
5353
}
5454

5555

56-
func createUser(email email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]? = nil) -> Request<DatabaseUser, AuthenticationError> {
57-
var payload: [String: AnyObject] = [
56+
func createUser(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]? = nil) -> Request<DatabaseUser, AuthenticationError> {
57+
var payload: [String: Any] = [
5858
"email": email,
5959
"password": password,
6060
"connection": connection,
@@ -63,81 +63,81 @@ struct Auth0Authentication: Authentication {
6363
payload["username"] = username
6464
payload["user_metadata"] = userMetadata
6565

66-
let createUser = NSURL(string: "/dbconnections/signup", relativeToURL: self.url)!
66+
let createUser = URL(string: "/dbconnections/signup", relativeTo: self.url)!
6767
return Request(session: session, url: createUser, method: "POST", handle: databaseUser, payload: payload, logger: self.logger, telemetry: self.telemetry)
6868
}
6969

70-
func resetPassword(email email: String, connection: String) -> Request<Void, AuthenticationError> {
70+
func resetPassword(email: String, connection: String) -> Request<Void, AuthenticationError> {
7171
let payload = [
7272
"email": email,
7373
"connection": connection,
7474
"client_id": self.clientId
7575
]
76-
let resetPassword = NSURL(string: "/dbconnections/change_password", relativeToURL: self.url)!
76+
let resetPassword = URL(string: "/dbconnections/change_password", relativeTo: self.url)!
7777
return Request(session: session, url: resetPassword, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
7878
}
7979

80-
func signUp(email email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: AnyObject]?, scope: String, parameters: [String: AnyObject]) -> ConcatRequest<DatabaseUser, Credentials, AuthenticationError> {
80+
func signUp(email: String, username: String? = nil, password: String, connection: String, userMetadata: [String: Any]?, scope: String, parameters: [String: Any]) -> ConcatRequest<DatabaseUser, Credentials, AuthenticationError> {
8181
let first = createUser(email: email, username: username, password: password, connection: connection, userMetadata: userMetadata)
8282
let second = login(usernameOrEmail: email, password: password, connection: connection, scope: scope, parameters: parameters)
8383
return ConcatRequest(first: first, second: second)
8484
}
8585

86-
func startPasswordless(email email: String, type: PasswordlessType, connection: String, parameters: [String: AnyObject]) -> Request<Void, AuthenticationError> {
87-
var payload: [String: AnyObject] = [
86+
func startPasswordless(email: String, type: PasswordlessType, connection: String, parameters: [String: Any]) -> Request<Void, AuthenticationError> {
87+
var payload: [String: Any] = [
8888
"email": email,
8989
"connection": connection,
9090
"send": type.rawValue,
9191
"client_id": self.clientId,
9292
]
93-
if case .WebLink = type where !parameters.isEmpty {
93+
if case .WebLink = type , !parameters.isEmpty {
9494
payload["authParams"] = parameters
9595
}
9696

97-
let start = NSURL(string: "/passwordless/start", relativeToURL: self.url)!
97+
let start = URL(string: "/passwordless/start", relativeTo: self.url)!
9898
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
9999
}
100100

101-
func startPasswordless(phoneNumber phoneNumber: String, type: PasswordlessType, connection: String) -> Request<Void, AuthenticationError> {
102-
let payload: [String: AnyObject] = [
101+
func startPasswordless(phoneNumber: String, type: PasswordlessType, connection: String) -> Request<Void, AuthenticationError> {
102+
let payload: [String: Any] = [
103103
"phone_number": phoneNumber,
104104
"connection": connection,
105105
"send": type.rawValue,
106106
"client_id": self.clientId,
107107
]
108-
let start = NSURL(string: "/passwordless/start", relativeToURL: self.url)!
108+
let start = URL(string: "/passwordless/start", relativeTo: self.url)!
109109
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
110110
}
111111

112-
func tokenInfo(token token: String) -> Request<Profile, AuthenticationError> {
113-
let payload: [String: AnyObject] = ["id_token": token]
114-
let tokenInfo = NSURL(string: "/tokeninfo", relativeToURL: self.url)!
112+
func tokenInfo(token: String) -> Request<Profile, AuthenticationError> {
113+
let payload: [String: Any] = ["id_token": token]
114+
let tokenInfo = URL(string: "/tokeninfo", relativeTo: self.url)!
115115
return Request(session: session, url: tokenInfo, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
116116
}
117117

118-
func userInfo(token token: String) -> Request<Profile, AuthenticationError> {
119-
let userInfo = NSURL(string: "/userinfo", relativeToURL: self.url)!
118+
func userInfo(token: String) -> Request<Profile, AuthenticationError> {
119+
let userInfo = URL(string: "/userinfo", relativeTo: self.url)!
120120
return Request(session: session, url: userInfo, method: "GET", handle: authenticationObject, headers: ["Authorization": "Bearer \(token)"], logger: self.logger, telemetry: self.telemetry)
121121
}
122122

123-
func loginSocial(token token: String, connection: String, scope: String, parameters: [String: AnyObject]) -> Request<Credentials, AuthenticationError> {
124-
var payload: [String: AnyObject] = [
123+
func loginSocial(token: String, connection: String, scope: String, parameters: [String: Any]) -> Request<Credentials, AuthenticationError> {
124+
var payload: [String: Any] = [
125125
"access_token": token,
126126
"connection": connection,
127127
"scope": scope,
128128
"client_id": self.clientId,
129129
]
130130
parameters.forEach { key, value in payload[key] = value }
131-
let accessToken = NSURL(string: "/oauth/access_token", relativeToURL: self.url)!
131+
let accessToken = URL(string: "/oauth/access_token", relativeTo: self.url)!
132132
return Request(session: session, url: accessToken, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
133133
}
134134

135-
func tokenExchange(withParameters parameters: [String: AnyObject]) -> Request<Credentials, AuthenticationError> {
136-
var payload: [String: AnyObject] = [
137-
"client_id": self.clientId
135+
func tokenExchange(withParameters parameters: [String: Any]) -> Request<Credentials, AuthenticationError> {
136+
var payload: [String: Any] = [
137+
"client_id": self.clientId,
138138
]
139139
parameters.forEach { payload[$0] = $1 }
140-
let token = NSURL(string: "/oauth/token", relativeToURL: self.url)!
140+
let token = URL(string: "/oauth/token", relativeTo: self.url)!
141141
return Request(session: session, url: token, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
142142
}
143143

@@ -146,7 +146,7 @@ struct Auth0Authentication: Authentication {
146146
"code": code,
147147
"code_verifier": codeVerifier,
148148
"redirect_uri": redirectURI,
149-
"grant_type": "authorization_code"
149+
"grant_type": "authorization_code",
150150
])
151151
}
152152
}

Auth0/Auth0Error.swift

+2-9
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,12 @@ let EmptyBodyError = "a0.sdk.internal_error.empty"
3030
Generic representation of Auth0 API errors
3131
- note: It's recommended to use either `AuthenticationError` or `ManagementError` for better error handling
3232
*/
33-
public protocol Auth0Error: ErrorType {
33+
public protocol Auth0Error: Error {
3434

3535
init(string: String?, statusCode: Int)
36-
init(info: [String: AnyObject], statusCode: Int)
36+
init(info: [String: Any], statusCode: Int)
3737

3838
/// The code of the error as a String
3939
var code: String { get }
4040

4141
}
42-
43-
internal protocol FoundationErrorConvertible {
44-
static var FoundationDomain: String { get }
45-
static var FoundationUserInfoKey: String { get }
46-
47-
func newFoundationError() -> NSError
48-
}

0 commit comments

Comments
 (0)