-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from journeyhealth/add-auth0-support
Add Auth0 support
- Loading branch information
Showing
9 changed files
with
72 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Vapor | ||
import Foundation | ||
|
||
public class Auth0Router: FederatedServiceRouter { | ||
|
||
public let baseURL: String | ||
public let tokens: FederatedServiceTokens | ||
public let callbackCompletion: (Request, String) throws -> (EventLoopFuture<ResponseEncodable>) | ||
public var scope: [String] = [ ] | ||
public var requiredScopes = [ "openid" ] | ||
public let callbackURL: String | ||
public let accessTokenURL: String | ||
public var service: OAuthService = .auth0 | ||
public let callbackHeaders = HTTPHeaders([("Content-Type", "application/x-www-form-urlencoded")]) | ||
|
||
private func providerUrl(path: String) -> String { | ||
return self.baseURL.finished(with: "/") + path | ||
} | ||
|
||
public required init(callback: String, completion: @escaping (Request, String) throws -> (EventLoopFuture<ResponseEncodable>)) throws { | ||
let auth = try Auth0Auth() | ||
self.tokens = auth | ||
self.baseURL = "https://\(auth.domain)" | ||
self.accessTokenURL = baseURL.finished(with: "/") + "oauth/token" | ||
self.callbackURL = callback | ||
self.callbackCompletion = completion | ||
} | ||
|
||
public func authURL(_ request: Request) throws -> String { | ||
let path="authorize" | ||
|
||
var params=[ | ||
"response_type=code", | ||
"client_id=\(self.tokens.clientID)", | ||
"redirect_uri=\(self.callbackURL)", | ||
] | ||
|
||
let allScopes = self.scope + self.requiredScopes | ||
let scopeString = allScopes.joined(separator: " ").addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) | ||
if let scopes = scopeString { | ||
params += [ "scope=\(scopes)" ] | ||
} | ||
|
||
let rtn = self.providerUrl(path: path + "?" + params.joined(separator: "&")) | ||
return rtn | ||
} | ||
|
||
public func callbackBody(with code: String) -> ResponseEncodable { | ||
Auth0CallbackBody(clientId: self.tokens.clientID, | ||
clientSecret: self.tokens.clientSecret, | ||
code: code, | ||
redirectURI: self.callbackURL) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...perial/Services/Auth0/Service+Auth0.swift → Sources/ImperialAuth0/Service+Auth0.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters