This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.List; | |
public class Numbers { | |
public static void main(String[] args) { | |
int[] numbers = { 95017, 12059, 594, 524, 867, 777, 192, 93707, 51489, 43046, 25, 66, 68240 }; | |
List<Integer> answers = new ArrayList<>(); | |
// Answer: 777, 68240 |
This file contains hidden or 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
import JWT | |
enum JWTConfig { | |
static let signerKey = "JWT_API_SIGNER_KEY" // Key for signing JWT Access Token | |
static let header = JWTHeader(alg: "HS256", typ: "JWT") // Algorithm and Type | |
static let signer = JWTSigner.hs256(key: JWTConfig.signerKey) // Signer for JWT | |
static let expirationTime: TimeInterval = 100 // In seconds | |
} |
This file contains hidden or 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
import JWT | |
extension JWTError { | |
static let payloadCreation = JWTError(identifier: "TokenHelpers.createPayload", reason: "User ID not found") | |
static let createJWT = JWTError(identifier: "TokenHelpers.createJWT", reason: "Error getting token string") | |
static let verificationFailed = JWTError(identifier: "TokenHelpers.verifyToken", reason: "JWT verification failed") | |
} |
This file contains hidden or 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
import JWT | |
struct AccessTokenPayload: JWTPayload { | |
var issuer: IssuerClaim | |
var issuedAt: IssuedAtClaim | |
var expirationAt: ExpirationClaim | |
var userID: User.ID | |
init(issuer: String = "TokensTutorial", |
This file contains hidden or 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
import JWT | |
class TokenHelpers { | |
/// Create payload for Access Token | |
fileprivate class func createPayload(from user: User) throws -> AccessTokenPayload { | |
if let id = user.id { | |
let payload = AccessTokenPayload(userID: id) | |
return payload |
This file contains hidden or 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
import Vapor | |
struct AccessDto: Content { | |
let accessToken: String | |
let expiredAt: Date | |
} |
This file contains hidden or 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
func signIn(request: Request, user: User) throws -> Future<AccessDto> { | |
return User | |
.query(on: request) | |
.filter(\.login == user.login) | |
.first() | |
.unwrap(or: Abort(.badRequest, reason: "User with login \(user.login) not found")) | |
.flatMap { persistedUser in | |
let digest = try request.make(BCryptDigest.self) | |
if try digest.verify(user.password, created: persistedUser.password) { |
This file contains hidden or 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
import Vapor | |
import JWT | |
class JWTMiddleware: Middleware { | |
func respond(to request: Request, chainingTo next: Responder) throws -> EventLoopFuture<Response> { | |
if let token = request.http.headers[.authorization].first { | |
do { | |
try TokenHelpers.verifyToken(token) | |
return try next.respond(to: request) |
This file contains hidden or 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
import Vapor | |
import JWT | |
extension Request { | |
var token: String { | |
if let token = self.http.headers[.authorization].first { | |
return token | |
} else { | |
return "" |
This file contains hidden or 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
import Vapor | |
struct TodoDto: Content { | |
let id: Int? | |
let title: String | |
} |
OlderNewer