-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from Hexaville/v0.2
v0.2
- Loading branch information
Showing
5 changed files
with
171 additions
and
88 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 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,29 @@ | ||
// | ||
// AuthenticationMiddleware.swift | ||
// HexavilleAuth | ||
// | ||
// Created by Yuki Takei on 2017/06/25. | ||
// | ||
// | ||
|
||
import Foundation | ||
import HexavilleFramework | ||
|
||
|
||
extension HexavilleAuth { | ||
public struct AuthenticationMiddleware: Middleware { | ||
|
||
public init(){} | ||
|
||
static var sessionKey = "hexaville.auth.loginUser" | ||
|
||
public func respond(to request: Request, context: ApplicationContext) throws -> Chainer { | ||
if let dict = context.session?[AuthenticationMiddleware.sessionKey] as? [String: Any] { | ||
context.loginUser = try LoginUser(fromDictionary: dict) | ||
} | ||
|
||
return .next(request) | ||
} | ||
} | ||
|
||
} |
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,74 @@ | ||
// | ||
// HexavilleAuth+Router.swift | ||
// HexavilleAuth | ||
// | ||
// Created by Yuki Takei on 2017/06/25. | ||
// | ||
// | ||
|
||
import Foundation | ||
import HexavilleFramework | ||
|
||
extension HexavilleAuth { | ||
func asRouter() -> Router { | ||
let router = Router() | ||
for type in providers { | ||
switch type { | ||
case .oauth1(let provider): | ||
router.use(.get, provider.path) { request, context in | ||
let requestToken = try provider.getRequestToken() | ||
context.session?["hexaville.oauth_token_secret"] = requestToken.oauthTokenSecret | ||
context.session?["hexaville.oauth_token"] = requestToken.oauthToken | ||
let location = try provider.createAuthorizeURL(requestToken: requestToken).absoluteString | ||
|
||
return Response(status: .found, headers: ["Location": location]) | ||
} | ||
|
||
router.use(.get, provider.oauth.callbackURL.path) { request, context in | ||
guard let secret = context.session?["hexaville.oauth_token_secret"] as? String else { | ||
throw OAuth1Error.accessTokenIsMissingInSession | ||
} | ||
|
||
guard let token = context.session?["hexaville.oauth_token"] as? String else { | ||
throw OAuth1Error.accessTokenIsMissingInSession | ||
} | ||
|
||
let requestToken = RequestToken( | ||
oauthToken: token, | ||
oauthTokenSecret: secret, | ||
oauthCallbackConfirmed: nil | ||
) | ||
|
||
let (cred, user) = try provider.authorize(request: request, requestToken: requestToken) | ||
context.session?[AuthenticationMiddleware.sessionKey] = user.serialize() | ||
return try provider.callback(cred, user, request, context) | ||
} | ||
|
||
|
||
case .oauth2(let provider): | ||
router.use(.get, provider.path) { request, context in | ||
return Response( | ||
status: .found, | ||
headers: [ | ||
"Location": try provider.createAuthorizeURL().absoluteString | ||
] | ||
) | ||
} | ||
|
||
router.use(.get, provider.oauth.callbackURL.path) { request, context in | ||
let (cred, user) = try provider.authorize(request: request) | ||
context.session?[AuthenticationMiddleware.sessionKey] = user.serialize() | ||
return try provider.callback(cred, user, request, context) | ||
} | ||
} | ||
} | ||
|
||
return router | ||
} | ||
} | ||
|
||
extension HexavilleFramework { | ||
public func use(_ auth: HexavilleAuth) { | ||
self.use(auth.asRouter()) | ||
} | ||
} |
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