Skip to content

Commit

Permalink
Merge pull request #29 from auth0/chore-telemetry
Browse files Browse the repository at this point in the history
Add Telemetry info
  • Loading branch information
hzalaz authored Jun 21, 2016
2 parents b11de7c + 19a3735 commit 605bf37
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 18 deletions.
28 changes: 28 additions & 0 deletions Auth0.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
5FE2F8C71CD1522F003628F4 /* UserProfileSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE2F8C51CD1522F003628F4 /* UserProfileSpec.swift */; };
5FE686A11D1877C10075874C /* Auth0.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5FE686A01D1877C10075874C /* Auth0.plist */; };
5FE686A21D1877C10075874C /* Auth0.plist in Resources */ = {isa = PBXBuildFile; fileRef = 5FE686A01D1877C10075874C /* Auth0.plist */; };
5FE686A61D188DEF0075874C /* Telemetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE686A51D188DEF0075874C /* Telemetry.swift */; };
5FE686A71D188DEF0075874C /* Telemetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE686A51D188DEF0075874C /* Telemetry.swift */; };
5FE686AA1D1894AA0075874C /* TelemetrySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE686A91D1894AA0075874C /* TelemetrySpec.swift */; };
5FE686AB1D1894AA0075874C /* TelemetrySpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FE686A91D1894AA0075874C /* TelemetrySpec.swift */; };
5FF465BC1CE2AC4500F7ED8C /* Management.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FF465BB1CE2AC4500F7ED8C /* Management.swift */; };
5FF465BD1CE2AC4500F7ED8C /* Management.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FF465BB1CE2AC4500F7ED8C /* Management.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -257,6 +261,8 @@
5FE2F8C21CD1498E003628F4 /* UserProfile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserProfile.swift; sourceTree = "<group>"; };
5FE2F8C51CD1522F003628F4 /* UserProfileSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserProfileSpec.swift; sourceTree = "<group>"; };
5FE686A01D1877C10075874C /* Auth0.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Auth0.plist; sourceTree = "<group>"; };
5FE686A51D188DEF0075874C /* Telemetry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Telemetry.swift; sourceTree = "<group>"; };
5FE686A91D1894AA0075874C /* TelemetrySpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TelemetrySpec.swift; sourceTree = "<group>"; };
5FF346491CEFEC04000799DE /* Auth0Tests.iOS-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Auth0Tests.iOS-Bridging-Header.h"; sourceTree = "<group>"; };
5FF3464A1CEFEC04000799DE /* Auth0Tests.OSX-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Auth0Tests.OSX-Bridging-Header.h"; sourceTree = "<group>"; };
5FF465BB1CE2AC4500F7ED8C /* Management.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Management.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -336,6 +342,7 @@
5F049B6B1CB42C29006F6C05 /* Auth0 */ = {
isa = PBXGroup;
children = (
5FE686A41D188DA50075874C /* Telemetry */,
5FBBF03D1CCA8FC20024D2AF /* Authentication */,
5FCAB16E1D08FFE900331C84 /* Extensions */,
5F6FAC611D09E92800D5B4EA /* Logger */,
Expand All @@ -353,6 +360,7 @@
5F06DD921CC451430011842B /* Auth0Tests */ = {
isa = PBXGroup;
children = (
5FE686A81D1894990075874C /* Telemetry */,
5FBBF0411CCA901B0024D2AF /* Authentication */,
5FADB6011CEC0C1600D4BB50 /* Management */,
5FE2F8C11CD0FA11003628F4 /* Networking */,
Expand Down Expand Up @@ -565,6 +573,22 @@
path = Networking;
sourceTree = "<group>";
};
5FE686A41D188DA50075874C /* Telemetry */ = {
isa = PBXGroup;
children = (
5FE686A51D188DEF0075874C /* Telemetry.swift */,
);
name = Telemetry;
sourceTree = "<group>";
};
5FE686A81D1894990075874C /* Telemetry */ = {
isa = PBXGroup;
children = (
5FE686A91D1894AA0075874C /* TelemetrySpec.swift */,
);
name = Telemetry;
sourceTree = "<group>";
};
5FF465BA1CE2AC2800F7ED8C /* Management */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -821,6 +845,7 @@
5FF465BC1CE2AC4500F7ED8C /* Management.swift in Sources */,
5F4A1F961D00AABC00C72242 /* OAuth2Grant.swift in Sources */,
5FCAB1731D09009600331C84 /* NSData+URLSafe.swift in Sources */,
5FE686A61D188DEF0075874C /* Telemetry.swift in Sources */,
5FD255BA1D14F70B00387ECB /* WebAuthError.swift in Sources */,
5FD255B71D14F00900387ECB /* Auth0Error.swift in Sources */,
5FD255AE1D14A4A300387ECB /* AuthenticationError.swift in Sources */,
Expand Down Expand Up @@ -854,6 +879,7 @@
5FBBF0471CCA99E90024D2AF /* Credentials.swift in Sources */,
5F74CB3E1CEE8E9100226823 /* UserIdentity.swift in Sources */,
5F6FAC641D09E98000D5B4EA /* Logger.swift in Sources */,
5FE686A71D188DEF0075874C /* Telemetry.swift in Sources */,
5FF465BD1CE2AC4500F7ED8C /* Management.swift in Sources */,
5F06DDCA1CC66B710011842B /* Auth0.swift in Sources */,
5FD255B81D14F00900387ECB /* Auth0Error.swift in Sources */,
Expand Down Expand Up @@ -881,6 +907,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5FE686AA1D1894AA0075874C /* TelemetrySpec.swift in Sources */,
5FCAB1691D07AC3500331C84 /* A0WebAuthSpec.m in Sources */,
5FBBF03B1CC96AA70024D2AF /* Responses.swift in Sources */,
5FCAB1681D07AC2700331C84 /* ControllerModalPresenterSpec.swift in Sources */,
Expand All @@ -907,6 +934,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5FE686AB1D1894AA0075874C /* TelemetrySpec.swift in Sources */,
5FBBF03C1CC96AA70024D2AF /* Responses.swift in Sources */,
5FADB6101CED7E5200D4BB50 /* UserPatchAttributesSpec.swift in Sources */,
5FBBF0391CC964BC0024D2AF /* Matchers.swift in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions Auth0/Auth0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ public func enableLogging(enabled enabled: Bool = true) {
Auth0Logger.sharedInstance.logger = enabled ? DefaultLogger() : nil
}

/**
Avoid Auth0.swift sending its version on every request to Auth0 API.
By default we collect our libraries and SDKs versions to help us during support and evaluate usage.

- parameter enabled: if Auth0.swift should send it's version on every request.
*/
public func enableTelemetry(enabled enabled: Bool) {
Telemetry.sharedInstance.enabled = enabled
}

func plistValues(bundle bundle: NSBundle) -> (clientId: String, domain: String)? {
guard
let path = bundle.pathForResource("Auth0", ofType: "plist"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ public class _ObjectiveAuthenticationAPI: NSObject {
.start(handleResult(callback))
}

/**
Avoid Auth0.swift sending its version on every request to Auth0 API.
By default we collect our libraries and SDKs versions to help us during support and evaluate usage.

- parameter enabled: if Auth0.swift should send it's version on every request.
*/
public func setTelemetryEnabled(enabled: Bool) {
Telemetry.sharedInstance.enabled = enabled
}
}

private func handleResult<T>(callback: (NSError?, T?) -> ()) -> Result<T> -> () {
Expand Down
9 changes: 9 additions & 0 deletions Auth0/Management/ObjectiveC/_ObjectiveManagementAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ public class _ObjectiveManagementAPI: NSObject {
}
}

/**
Avoid Auth0.swift sending its version on every request to Auth0 API.
By default we collect our libraries and SDKs versions to help us during support and evaluate usage.

- parameter enabled: if Auth0.swift should send it's version on every request.
*/
public func setTelemetryEnabled(enabled: Bool) {
Telemetry.sharedInstance.enabled = enabled
}
}
5 changes: 4 additions & 1 deletion Auth0/Networking/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public struct Request<T, E: Auth0Error>: Requestable {
let payload: [String: AnyObject]
let headers: [String: String]
let logger: Logger?
let telemetry: Telemetry

init(session: NSURLSession, url: NSURL, method: String, handle: (Response<E>, Callback) -> (), payload: [String: AnyObject] = [:], headers: [String: String] = [:], logger: Logger? = Auth0Logger.sharedInstance.logger) {
init(session: NSURLSession, url: NSURL, method: String, handle: (Response<E>, Callback) -> (), payload: [String: AnyObject] = [:], headers: [String: String] = [:], logger: Logger? = Auth0Logger.sharedInstance.logger, telemetry: Telemetry = Telemetry.sharedInstance) {
self.session = session
self.url = url
self.method = method
self.handle = handle
self.payload = payload
self.headers = headers
self.logger = logger
self.telemetry = telemetry
}

var request: NSURLRequest {
Expand All @@ -68,6 +70,7 @@ public struct Request<T, E: Auth0Error>: Requestable {
}
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
headers.forEach { name, value in request.setValue(value, forHTTPHeaderField: name) }
telemetry.addTelemetryHeader(request: request)
return request
}

Expand Down
89 changes: 89 additions & 0 deletions Auth0/Telemetry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Telemetry.swift
//
// Copyright (c) 2016 Auth0 (http://auth0.com)
//
// 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.

import Foundation

class Telemetry: NSObject {

static let NameKey = "name"
static let VersionKey = "version"
static let WrappedVersion = "lib_version"

static let NoVersion = "0.0.0"
static let LibraryName = "Auth0.swift"

static let sharedInstance = Telemetry()

var enabled: Bool = true

var info: String?

var value: String? {
get {
return self.enabled ? self.info : nil
}
}

override init() {
self.info = Telemetry.generateValue()
}

func wrapped(inLibrary name: String, version: String) {
let info = Telemetry.versionInformation()
let wrapped = [
Telemetry.NameKey: name,
Telemetry.VersionKey: version,
Telemetry.WrappedVersion: info[Telemetry.VersionKey] ?? Telemetry.NoVersion
]
self.info = Telemetry.generateValue(fromInfo: wrapped)
}

func addTelemetryHeader(request request: NSMutableURLRequest) {
if let value = self.value {
request.setValue(value, forHTTPHeaderField: "Auth0-Client")
} else {
request.setValue(nil, forHTTPHeaderField: "Auth0-Client")
}
}

func queryItemsWithTelemetry(queryItems queryItems: [NSURLQueryItem]) -> [NSURLQueryItem] {
var items = queryItems
if let value = self.value {
items.append(NSURLQueryItem(name: "auth0Client", value: value))
}
return items
}

static func versionInformation(bundle bundle: NSBundle = NSBundle(forClass: Telemetry.classForCoder())) -> [String: String] {
let version = bundle.infoDictionary?["CFBundleShortVersionString"] as? String ?? Telemetry.NoVersion
let dict = [
Telemetry.NameKey: Telemetry.LibraryName,
Telemetry.VersionKey: version,
]
return dict
}

static func generateValue(fromInfo info: [String: String] = Telemetry.versionInformation()) -> String? {
let data = try? NSJSONSerialization.dataWithJSONObject(info, options: [])
return data?.a0_encodeBase64URLSafe()
}
}
10 changes: 10 additions & 0 deletions Auth0/WebAuth/ObjectiveC/_ObjectiveWebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,14 @@ public class _ObjectiveOAuth2: NSObject {
public static func resume(url: NSURL, options: [String: AnyObject]) -> Bool {
return SessionStorage.sharedInstance.resume(url, options: options)
}

/**
Avoid Auth0.swift sending its version on every request to Auth0 API.
By default we collect our libraries and SDKs versions to help us during support and evaluate usage.

- parameter enabled: if Auth0.swift should send it's version on every request.
*/
public func setTelemetryEnabled(enabled: Bool) {
Telemetry.sharedInstance.enabled = enabled
}
}
7 changes: 5 additions & 2 deletions Auth0/WebAuth/WebAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class WebAuth {
let presenter: ControllerModalPresenter
let storage: SessionStorage
let logger: Logger?
let telemetry: Telemetry
var state = generateDefaultState()
var parameters: [String: String] = [:]
var universalLink = false
Expand All @@ -102,13 +103,15 @@ public class WebAuth {
self.init(clientId: clientId, url: url, presenter: presenter, storage: SessionStorage.sharedInstance)
}

init(clientId: String, url: NSURL, presenter: ControllerModalPresenter, storage: SessionStorage, logger: Logger? = Auth0Logger.sharedInstance.logger) {
init(clientId: String, url: NSURL, presenter: ControllerModalPresenter, storage: SessionStorage, logger: Logger? = Auth0Logger.sharedInstance.logger, telemetry: Telemetry = Telemetry.sharedInstance) {
self.clientId = clientId
self.url = url
self.presenter = presenter
self.storage = storage
self.logger = logger
self.telemetry = telemetry
}

/**
For redirect url instead of a custom scheme it will use `https` and iOS 9 Universal Links.

Expand Down Expand Up @@ -259,7 +262,7 @@ public class WebAuth {
let addAll: (String, String) -> () = { items.append(NSURLQueryItem(name: $0, value: $1)) }
defaults.forEach(addAll)
self.parameters.forEach(addAll)
components.queryItems = items
components.queryItems = self.telemetry.queryItemsWithTelemetry(queryItems: items)
return components.URL!
}

Expand Down
16 changes: 1 addition & 15 deletions Auth0Tests/OAuth2/ChallengeGeneratorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,4 @@ class ChallengeGeneratorSpec: QuickSpec {
}

}
}

func beURLSafeBase64() -> MatcherFunc<String> {
return MatcherFunc { expression, failureMessage in
failureMessage.postfixMessage = "be url safe base64"
let set = NSMutableCharacterSet()
set.formUnionWithCharacterSet(.alphanumericCharacterSet())
set.addCharactersInString("-_/")
set.invert()
if let actual = try expression.evaluate() where actual.rangeOfCharacterFromSet(set) == nil {
return true
}
return false
}
}
}
Loading

0 comments on commit 605bf37

Please sign in to comment.