Skip to content

Commit

Permalink
Merge pull request #43 from auth0/feature-rework-logging
Browse files Browse the repository at this point in the history
Rework Logging
  • Loading branch information
hzalaz authored Sep 9, 2016
2 parents 7aaabdf + 235207d commit 7757181
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 77 deletions.
1 change: 0 additions & 1 deletion App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
Auth0.enableLogging()
return true
}

Expand Down
11 changes: 9 additions & 2 deletions App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ class ViewController: UIViewController {
}

@IBAction func startOAuth2(sender: AnyObject) {
Auth0.webAuth().start(onAuth)
var auth0 = Auth0.webAuth()
auth0
.logging(enabled: true)
.start(onAuth)
}

@IBAction func startGoogleOAuth2(sender: AnyObject) {
Auth0.webAuth().connection("google-oauth2").start(onAuth)
var auth0 = Auth0.webAuth()
auth0
.logging(enabled: true)
.connection("google-oauth2")
.start(onAuth)
}
}
33 changes: 27 additions & 6 deletions Auth0.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@
5F23E7131D4B890500C3F2D9 /* A0ChallengeGenerator.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FC34AF61D0101BF000F28F5 /* A0ChallengeGenerator.m */; };
5F23E71A1D4B891E00C3F2D9 /* Auth0.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F06DDC81CC66B710011842B /* Auth0.swift */; };
5F23E71B1D4B891E00C3F2D9 /* Auth0Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FD255B61D14F00900387ECB /* Auth0Error.swift */; };
5F2BD4041D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F2BD4031D09F7B700B5F7D2 /* _ObjectiveLogger.swift */; };
5F2BD4051D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F2BD4031D09F7B700B5F7D2 /* _ObjectiveLogger.swift */; };
5F28B4611D8216180000EB23 /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4601D8216180000EB23 /* Loggable.swift */; };
5F28B4621D8216180000EB23 /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4601D8216180000EB23 /* Loggable.swift */; };
5F28B4631D8216180000EB23 /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4601D8216180000EB23 /* Loggable.swift */; };
5F28B4641D8216180000EB23 /* Loggable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4601D8216180000EB23 /* Loggable.swift */; };
5F28B4671D8300D50000EB23 /* LoggerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4661D8300D50000EB23 /* LoggerSpec.swift */; };
5F28B4681D8300D50000EB23 /* LoggerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4661D8300D50000EB23 /* LoggerSpec.swift */; };
5F28B4691D8300D50000EB23 /* LoggerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F28B4661D8300D50000EB23 /* LoggerSpec.swift */; };
5F331AFE1D4BB24C00AE4382 /* Auth0.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F23E6F61D4B87F000C3F2D9 /* Auth0.framework */; };
5F331B041D4BB78C00AE4382 /* TelemetrySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE686A91D1894AA0075874C /* TelemetrySpec.swift */; };
5F331B051D4BB7D400AE4382 /* AuthenticationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FBBF0421CCA90300024D2AF /* AuthenticationSpec.swift */; };
Expand Down Expand Up @@ -298,7 +303,8 @@
5F23E6B91D4ACA7100C3F2D9 /* Auth0.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Auth0.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5F23E6F61D4B87F000C3F2D9 /* Auth0.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Auth0.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5F23E6FA1D4B87F000C3F2D9 /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = "Info-tvOS.plist"; path = "Auth0/Info-tvOS.plist"; sourceTree = SOURCE_ROOT; };
5F2BD4031D09F7B700B5F7D2 /* _ObjectiveLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = _ObjectiveLogger.swift; sourceTree = "<group>"; };
5F28B4601D8216180000EB23 /* Loggable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Loggable.swift; sourceTree = "<group>"; };
5F28B4661D8300D50000EB23 /* LoggerSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoggerSpec.swift; sourceTree = "<group>"; };
5F331AF91D4BB24C00AE4382 /* Auth0Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Auth0Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5F331B121D4BCBD400AE4382 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Quick.framework; path = Carthage/Build/tvOS/Quick.framework; sourceTree = "<group>"; };
5F331B131D4BCBD400AE4382 /* Nimble.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Nimble.framework; path = Carthage/Build/tvOS/Nimble.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -492,6 +498,7 @@
5F06DD921CC451430011842B /* Auth0Tests */ = {
isa = PBXGroup;
children = (
5F28B4651D8300BB0000EB23 /* Logger */,
5FE686A81D1894990075874C /* Telemetry */,
5FBBF0411CCA901B0024D2AF /* Authentication */,
5FADB6011CEC0C1600D4BB50 /* Management */,
Expand Down Expand Up @@ -538,6 +545,14 @@
name = OSX;
sourceTree = "<group>";
};
5F28B4651D8300BB0000EB23 /* Logger */ = {
isa = PBXGroup;
children = (
5F28B4661D8300D50000EB23 /* LoggerSpec.swift */,
);
name = Logger;
sourceTree = "<group>";
};
5F331B111D4BB8D700AE4382 /* tvOS */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -579,7 +594,7 @@
isa = PBXGroup;
children = (
5F6FAC621D09E98000D5B4EA /* Logger.swift */,
5F2BD4031D09F7B700B5F7D2 /* _ObjectiveLogger.swift */,
5F28B4601D8216180000EB23 /* Loggable.swift */,
);
path = Logger;
sourceTree = "<group>";
Expand Down Expand Up @@ -1091,6 +1106,7 @@
5FCCC31C1CF51DF300901E2E /* _ObjectiveManagementAPI.swift in Sources */,
5FC34AF81D0101BF000F28F5 /* A0ChallengeGenerator.m in Sources */,
5F6FAC631D09E98000D5B4EA /* Logger.swift in Sources */,
5F28B4611D8216180000EB23 /* Loggable.swift in Sources */,
5FBBF0461CCA99E90024D2AF /* Credentials.swift in Sources */,
5F53F5CE1CFD157300476A46 /* OAuth2Session.swift in Sources */,
5FBEF3DC1D079F2100D90941 /* _ObjectiveWebAuth.swift in Sources */,
Expand All @@ -1102,7 +1118,6 @@
5FD255BA1D14F70B00387ECB /* WebAuthError.swift in Sources */,
5FD255B71D14F00900387ECB /* Auth0Error.swift in Sources */,
5FD255AE1D14A4A300387ECB /* AuthenticationError.swift in Sources */,
5F2BD4041D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */,
5F06DDC91CC66B710011842B /* Auth0.swift in Sources */,
5FE2F8B21CCEAED8003628F4 /* Requestable.swift in Sources */,
5FCCC3191CF51BBD00901E2E /* NSError+Management.swift in Sources */,
Expand All @@ -1129,6 +1144,7 @@
files = (
5FE2F8BC1CD0EAAD003628F4 /* Response.swift in Sources */,
5FCCC31D1CF51DF300901E2E /* _ObjectiveManagementAPI.swift in Sources */,
5F28B4621D8216180000EB23 /* Loggable.swift in Sources */,
5FBBF0471CCA99E90024D2AF /* Credentials.swift in Sources */,
5F74CB3E1CEE8E9100226823 /* Identity.swift in Sources */,
5F6FAC641D09E98000D5B4EA /* Logger.swift in Sources */,
Expand All @@ -1144,7 +1160,6 @@
5FADB60D1CED7E0800D4BB50 /* UserPatchAttributes.swift in Sources */,
5F7989521CF13A20009E8A10 /* _ObjectiveAuthenticationAPI.swift in Sources */,
5F74CB411CEFD5E600226823 /* JSONObjectPayload.swift in Sources */,
5F2BD4051D09F7B700B5F7D2 /* _ObjectiveLogger.swift in Sources */,
5FD255AF1D14A4A300387ECB /* AuthenticationError.swift in Sources */,
5FE2F8B91CD0E910003628F4 /* Request.swift in Sources */,
5FE2F8AA1CCE54F1003628F4 /* Result.swift in Sources */,
Expand Down Expand Up @@ -1173,6 +1188,7 @@
5FE2F8A61CCA9C17003628F4 /* CredentialsSpec.swift in Sources */,
5FADB6091CED500900D4BB50 /* ManagementSpec.swift in Sources */,
5FCAB16D1D07AC3500331C84 /* WebAuthSpec.swift in Sources */,
5F28B4671D8300D50000EB23 /* LoggerSpec.swift in Sources */,
5FBBF0431CCA90300024D2AF /* AuthenticationSpec.swift in Sources */,
5FCAB16B1D07AC3500331C84 /* OAuth2GrantSpec.swift in Sources */,
5FD255B11D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */,
Expand All @@ -1198,6 +1214,7 @@
5FE2F8A71CCA9C17003628F4 /* CredentialsSpec.swift in Sources */,
5FD255B21D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */,
5FADB60A1CED500900D4BB50 /* ManagementSpec.swift in Sources */,
5F28B4681D8300D50000EB23 /* LoggerSpec.swift in Sources */,
5FBBF0441CCA90300024D2AF /* AuthenticationSpec.swift in Sources */,
5FE2F8C71CD1522F003628F4 /* ProfileSpec.swift in Sources */,
5FE2F8BF1CD0EC52003628F4 /* ResponseSpec.swift in Sources */,
Expand All @@ -1219,6 +1236,7 @@
5F23E6DD1D4ACD6100C3F2D9 /* NSURL+Auth0.swift in Sources */,
5F23E6D71D4ACD5B00C3F2D9 /* Credentials.swift in Sources */,
5F23E6E71D4ACD8500C3F2D9 /* Response.swift in Sources */,
5F28B4631D8216180000EB23 /* Loggable.swift in Sources */,
5F23E6E01D4ACD7F00C3F2D9 /* Management.swift in Sources */,
5F23E6DA1D4ACD5B00C3F2D9 /* Profile.swift in Sources */,
5F23E6D51D4ACD4C00C3F2D9 /* Telemetry.swift in Sources */,
Expand Down Expand Up @@ -1248,6 +1266,7 @@
5F23E7011D4B88DB00C3F2D9 /* Credentials.swift in Sources */,
5F23E71A1D4B891E00C3F2D9 /* Auth0.swift in Sources */,
5F23E7101D4B88FC00C3F2D9 /* Response.swift in Sources */,
5F28B4641D8216180000EB23 /* Loggable.swift in Sources */,
5F23E7091D4B88F600C3F2D9 /* Management.swift in Sources */,
5F23E7041D4B88DB00C3F2D9 /* Profile.swift in Sources */,
5F23E6FF1D4B88D400C3F2D9 /* Telemetry.swift in Sources */,
Expand All @@ -1273,6 +1292,7 @@
5F331B061D4BB7DA00AE4382 /* CredentialsSpec.swift in Sources */,
5F331B0B1D4BB7F900AE4382 /* UserPatchAttributesSpec.swift in Sources */,
5F331B051D4BB7D400AE4382 /* AuthenticationSpec.swift in Sources */,
5F28B4691D8300D50000EB23 /* LoggerSpec.swift in Sources */,
5F331B0C1D4BB7F900AE4382 /* UsersSpec.swift in Sources */,
5F331B0E1D4BB80700AE4382 /* Matchers.swift in Sources */,
5F331B071D4BB7E300AE4382 /* IdentitySpec.swift in Sources */,
Expand Down Expand Up @@ -1869,6 +1889,7 @@
5F331B021D4BB24C00AE4382 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
5F3965E11CF67DD800CDE7C0 /* Build configuration list for PBXNativeTarget "OAuth2" */ = {
isa = XCConfigurationList;
Expand Down
11 changes: 0 additions & 11 deletions Auth0/Auth0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,6 @@ public func users(token token: String, domain: String, session: NSURLSession = .
return management(token: token, domain: domain, session: session).users()
}

/**
Turn on/off Auth0.swift debug logging of HTTP requests and OAuth2 flow (iOS only).

- parameter enabled: optional flag to turn on/off logging
- note: By default all logging is **disabled**
- important: Logging should be turned on/off **before** making request to Auth0 for the flag to take effect.
*/
public func enableLogging(enabled enabled: Bool = true) {
Auth0Logger.sharedInstance.logger = enabled ? DefaultLogger() : nil
}

func plistValues(bundle bundle: NSBundle) -> (clientId: String, domain: String)? {
guard
let path = bundle.pathForResource("Auth0", ofType: "plist"),
Expand Down
21 changes: 11 additions & 10 deletions Auth0/Authentication/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ public typealias DatabaseUser = (email: String, username: String?, verified: Boo
Auth endpoints of Auth0
- seeAlso: [Auth0 Auth API docs](https://auth0.com/docs/api/authentication)
*/
public struct Authentication: Trackable {
public struct Authentication: Trackable, Loggable {
public let clientId: String
public let url: NSURL
public var telemetry: Telemetry
public var logger: Logger?

let session: NSURLSession

Expand Down Expand Up @@ -103,7 +104,7 @@ public struct Authentication: Trackable {
]
payload["mfa_code"] = multifactorCode
parameters.forEach { key, value in payload[key] = value }
return Request(session: session, url: resourceOwner, method: "POST", handle: authenticationObject, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: resourceOwner, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
}


Expand Down Expand Up @@ -154,7 +155,7 @@ public struct Authentication: Trackable {
payload["user_metadata"] = userMetadata

let createUser = NSURL(string: "/dbconnections/signup", relativeToURL: self.url)!
return Request(session: session, url: createUser, method: "POST", handle: databaseUser, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: createUser, method: "POST", handle: databaseUser, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand All @@ -179,7 +180,7 @@ public struct Authentication: Trackable {
"client_id": self.clientId
]
let resetPassword = NSURL(string: "/dbconnections/change_password", relativeToURL: self.url)!
return Request(session: session, url: resetPassword, method: "POST", handle: noBody, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: resetPassword, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand Down Expand Up @@ -273,7 +274,7 @@ public struct Authentication: Trackable {
}

let start = NSURL(string: "/passwordless/start", relativeToURL: self.url)!
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand Down Expand Up @@ -309,7 +310,7 @@ public struct Authentication: Trackable {
"client_id": self.clientId,
]
let start = NSURL(string: "/passwordless/start", relativeToURL: self.url)!
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: start, method: "POST", handle: noBody, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand All @@ -329,7 +330,7 @@ public struct Authentication: Trackable {
public func tokenInfo(token token: String) -> Request<Profile, AuthenticationError> {
let payload: [String: AnyObject] = ["id_token": token]
let tokenInfo = NSURL(string: "/tokeninfo", relativeToURL: self.url)!
return Request(session: session, url: tokenInfo, method: "POST", handle: authenticationObject, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: tokenInfo, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand All @@ -348,7 +349,7 @@ public struct Authentication: Trackable {
*/
public func userInfo(token token: String) -> Request<Profile, AuthenticationError> {
let userInfo = NSURL(string: "/userinfo", relativeToURL: self.url)!
return Request(session: session, url: userInfo, method: "GET", handle: authenticationObject, headers: ["Authorization": "Bearer \(token)"], telemetry: self.telemetry)
return Request(session: session, url: userInfo, method: "GET", handle: authenticationObject, headers: ["Authorization": "Bearer \(token)"], logger: self.logger, telemetry: self.telemetry)
}

/**
Expand Down Expand Up @@ -386,7 +387,7 @@ public struct Authentication: Trackable {
]
parameters.forEach { key, value in payload[key] = value }
let accessToken = NSURL(string: "/oauth/access_token", relativeToURL: self.url)!
return Request(session: session, url: accessToken, method: "POST", handle: authenticationObject, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: accessToken, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
}


Expand All @@ -411,7 +412,7 @@ public struct Authentication: Trackable {
]
parameters.forEach { payload[$0] = $1 }
let token = NSURL(string: "/oauth/token", relativeToURL: self.url)!
return Request(session: session, url: token, method: "POST", handle: authenticationObject, payload: payload, telemetry: self.telemetry)
return Request(session: session, url: token, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// _ObjectiveLogger.swift
// Loggable.swift
//
// Copyright (c) 2016 Auth0 (http://auth0.com)
//
Expand All @@ -22,26 +22,39 @@

import Foundation

@objc(A0Logger)
public class _ObjectiveLogger: NSObject {
public protocol Loggable {

var logger: Logger? { get set }

}

public extension Loggable {

/**
Turn on Auth0.swift debug logging of HTTP requests and OAuth2 flow (iOS only).
Turn on Auth0.swift debug logging of HTTP requests and OAuth2 flow (iOS only) with a custom logger.

- parameter logger: logger used to print log statements
- note: By default all logging is **disabled**
- important: Logging should be turned on/off **before** making request to Auth0 for the flag to take effect.
*/
public static func loggingEnabled() {
enableLogging(enabled: true)
mutating func usingLogger(logger: Logger) -> Self {
self.logger = logger
return self
}

/**
Turn on/off Auth0.swift debug logging of HTTP requests and OAuth2 flow (iOS only).

- parameter enabled: flag to turn on/off logging
- parameter enabled: optional flag to turn on/off logging
- note: By default all logging is **disabled**
- important: Logging should be turned on/off **before** making request to Auth0 for the flag to take effect.
*/
public static func loggingEnabled(enabled: Bool) {
enableLogging(enabled: enabled)
mutating func logging(enabled enabled: Bool) -> Self {
if enabled {
self.logger = DefaultLogger()
} else {
self.logger = nil
}
return self
}
}
Loading

0 comments on commit 7757181

Please sign in to comment.