Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Credentials Manager for tvOS and Mac Platforms #206

Merged
merged 4 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Auth0.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,30 @@ web_auth_files = [
'Auth0/SilentSafariViewController.swift',
'Auth0/NativeAuth.swift',
'Auth0/AuthProvider.swift',
'Auth0/CredentialsManager.swift',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this are referenced in s.osx.exclude_files. Why did you remove them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enabling it for tvOS also allows it for Mac

'Auth0/CredentialsManagerError.swift',
'Auth0/BioAuthentication.swift'
]

watchos_exclude_files = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like web_auth_files plus 2 entries. Is there a nicer way to define this array? i.e. joining the already defined base array.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly not, i've tried joining arrays etc it will just throw out an error in cocoapods when linting the lib.

'Auth0/_ObjectiveWebAuth.swift',
'Auth0/ControllerModalPresenter.swift',
'Auth0/OAuth2Grant.swift',
'Auth0/AuthTransaction.swift',
'Auth0/TransactionStore.swift',
'Auth0/WebAuth.swift',
'Auth0/WebAuthError.swift',
'Auth0/SafariWebAuth.swift',
'Auth0/AuthSession.swift',
'Auth0/SafariSession.swift',
'Auth0/SafariAuthenticationSession.swift',
'Auth0/SafariAuthenticationCallback.swift',
'Auth0/SilentSafariViewController.swift',
'Auth0/NativeAuth.swift',
'Auth0/AuthProvider.swift',
'Auth0/BioAuthentication.swift',
'Auth0/CredentialsManagerError.swift',
'Auth0/CredentialsManager.swift'
]

Pod::Spec.new do |s|
s.name = 'Auth0'
s.version = version
Expand All @@ -45,8 +64,10 @@ Pod::Spec.new do |s|
s.ios.dependency 'SimpleKeychain'
s.osx.source_files = 'Auth0/*.swift'
s.osx.exclude_files = web_auth_files
s.osx.dependency 'SimpleKeychain'
s.watchos.source_files = 'Auth0/*.swift'
s.watchos.exclude_files = web_auth_files
s.watchos.exclude_files = watchos_exclude_files
s.tvos.source_files = 'Auth0/*.swift'
s.tvos.exclude_files = web_auth_files
s.tvos.dependency 'SimpleKeychain'
end
508 changes: 504 additions & 4 deletions Auth0.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0930"
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
1 change: 1 addition & 0 deletions Auth0/Auth0Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ struct Auth0Authentication: Authentication {
return Request(session: session, url: resourceOwner, method: "POST", handle: authenticationObject, payload: payload, logger: self.logger, telemetry: self.telemetry)
}

// swiftlint:disable:next function_parameter_count
func login(usernameOrEmail username: String, password: String, realm: String, audience: String?, scope: String?, parameters: [String: Any]?) -> Request<Credentials, AuthenticationError> {
let resourceOwner = URL(string: "/oauth/token", relativeTo: self.url)!
var payload: [String: Any] = [
Expand Down
25 changes: 20 additions & 5 deletions Auth0/CredentialsManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@

import Foundation
import SimpleKeychain
#if os(iOS)
import LocalAuthentication
#endif

/// Credentials management utility
public struct CredentialsManager {

private let storage = A0SimpleKeychain()
private let storeKey = "credentials"
private let authentication: Authentication
#if os(iOS)
private var bioAuth: BioAuthentication?
#endif

/// Creates a new CredentialsManager instance
///
Expand All @@ -46,20 +50,24 @@ public struct CredentialsManager {
/// - title: main message to display in TouchID prompt
/// - cancelTitle: cancel message to display in TouchID prompt (iOS 10+)
/// - fallbackTitle: fallback message to display in TouchID prompt after a failed match
#if os(iOS)
@available(*, deprecated, message: "see enableBiometrics(withTitle title:, cancelTitle:, fallbackTitle:)")
public mutating func enableTouchAuth(withTitle title: String, cancelTitle: String? = nil, fallbackTitle: String? = nil) {
self.enableBiometrics(withTitle: title, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle)
}
#endif

/// Enable Biometric Authentication for additional securtity during credentials retrieval.
///
/// - Parameters:
/// - title: main message to display when Touch ID is used
/// - cancelTitle: cancel message to display when Touch ID is used (iOS 10+)
/// - fallbackTitle: fallback message to display when Touch ID is used after a failed match
#if os(iOS)
public mutating func enableBiometrics(withTitle title: String, cancelTitle: String? = nil, fallbackTitle: String? = nil) {
self.bioAuth = BioAuthentication(authContext: LAContext(), title: title, cancelTitle: cancelTitle, fallbackTitle: fallbackTitle)
}
#endif

/// Store credentials instance in keychain
///
Expand Down Expand Up @@ -106,6 +114,7 @@ public struct CredentialsManager {
/// - callback: callback with the user's credentials or the cause of the error.
/// - Important: This method only works for a refresh token obtained after auth with OAuth 2.0 API Authorization.
/// - Note: [Auth0 Refresh Tokens Docs](https://auth0.com/docs/tokens/refresh-token)
#if os(iOS)
public func credentials(withScope scope: String? = nil, callback: @escaping (CredentialsManagerError?, Credentials?) -> Void) {
guard self.hasValid() else { return callback(.noCredentials, nil) }
if let bioAuth = self.bioAuth {
Expand All @@ -120,6 +129,12 @@ public struct CredentialsManager {
self.retrieveCredentials(withScope: scope, callback: callback)
}
}
#else
public func credentials(withScope scope: String? = nil, callback: @escaping (CredentialsManagerError?, Credentials?) -> Void) {
guard self.hasValid() else { return callback(.noCredentials, nil) }
self.retrieveCredentials(withScope: scope, callback: callback)
}
#endif

private func retrieveCredentials(withScope scope: String? = nil, callback: @escaping (CredentialsManagerError?, Credentials?) -> Void) {
guard
Expand All @@ -134,11 +149,11 @@ public struct CredentialsManager {
switch $0 {
case .success(let credentials):
let newCredentials = Credentials(accessToken: credentials.accessToken,
tokenType: credentials.tokenType,
idToken: credentials.idToken,
refreshToken: refreshToken,
expiresIn: credentials.expiresIn,
scope: credentials.scope)
tokenType: credentials.tokenType,
idToken: credentials.idToken,
refreshToken: refreshToken,
expiresIn: credentials.expiresIn,
scope: credentials.scope)
_ = self.store(credentials: newCredentials)
callback(nil, newCredentials)
case .failure(let error):
Expand Down
4 changes: 4 additions & 0 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import Quick
import Nimble
import OHHTTPStubs
import SimpleKeychain
#if os(iOS)
import LocalAuthentication
#endif

@testable import Auth0

Expand Down Expand Up @@ -157,6 +159,7 @@ class CredentialsManagerSpec: QuickSpec {
expect(newCredentials).toEventuallyNot(beNil())
}

#if os(iOS)
context("require touch") {

beforeEach {
Expand All @@ -176,6 +179,7 @@ class CredentialsManagerSpec: QuickSpec {
}
}
}
#endif

context("renew") {

Expand Down
26 changes: 26 additions & 0 deletions OAuth2Mac/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// AppDelegate.swift
// OAuth2Mac
//
// Created by Martin on 16/07/2018.
// Copyright © 2018 Auth0. All rights reserved.
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {



func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


}

58 changes: 58 additions & 0 deletions OAuth2Mac/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"images" : [
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "16x16",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "32x32",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "128x128",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "256x256",
"scale" : "2x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "1x"
},
{
"idiom" : "mac",
"size" : "512x512",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions OAuth2Mac/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading