-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from brokenhandsio/remoteTokenSupport
Add default implementations for the Resource Server
- Loading branch information
Showing
3 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
Sources/VaporOAuthFluent/Managers/FluentResourceServerRetriever.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import VaporOAuth | ||
|
||
public struct FluentResourceServerRetriever: ResourceServerRetriever { | ||
|
||
public init() { } | ||
|
||
public func getServer(_ username: String) -> OAuthResourceServer? { | ||
return (try? OAuthResourceServer.makeQuery().filter(OAuthResourceServer.Properties.username, username).first()) ?? nil | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
Sources/VaporOAuthFluent/Models/OAuthResourceServer+Fluent.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import VaporOAuth | ||
import FluentProvider | ||
|
||
extension OAuthResourceServer: Model { | ||
|
||
struct Properties { | ||
static let username = "username" | ||
static let password = "password" | ||
} | ||
|
||
public var storage: Storage { | ||
if let storage = extend["fluent-storage"] as? Storage { | ||
return storage | ||
} else { | ||
let storage = Storage() | ||
extend["fluent-storage"] = storage | ||
return storage | ||
} | ||
} | ||
|
||
public convenience init(row: Row) throws { | ||
let username: String = try row.get(Properties.username) | ||
let passwordAsString: String = try row.get(Properties.password) | ||
self.init(username: username, password: passwordAsString.makeBytes()) | ||
} | ||
|
||
public func makeRow() throws -> Row { | ||
var row = Row() | ||
try row.set(Properties.username, username) | ||
try row.set(Properties.password, password.makeString()) | ||
return row | ||
} | ||
} | ||
|
||
extension OAuthResourceServer: Preparation { | ||
public static func prepare(_ database: Database) throws { | ||
try database.create(self) { builder in | ||
builder.id() | ||
builder.string(Properties.username) | ||
builder.string(Properties.password) | ||
} | ||
|
||
try database.index(Properties.username, for: OAuthResourceServer.self) | ||
} | ||
|
||
public static func revert(_ database: Database) throws { | ||
try database.delete(self) | ||
} | ||
} |
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