generated from video-trading/release-template
-
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.
* feat: add jwt support for services * feat: add jwt token support
- Loading branch information
Showing
21 changed files
with
308 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
setupFilesAfterEnv: ["<rootDir>/setup.ts"], | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import * as crypto from "crypto"; | ||
|
||
//@ts-ignore | ||
global.crypto = { | ||
subtle: crypto.webcrypto.subtle, | ||
}; |
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
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
31 changes: 31 additions & 0 deletions
31
packages/common/Sources/common/Middlewares/JWTMiddleWare.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,31 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Qiwei Li on 7/18/22. | ||
// | ||
|
||
import Foundation | ||
import Vapor | ||
import JWT | ||
import JWTKit | ||
|
||
|
||
public class JWTMiddleWare: AsyncMiddleware { | ||
public init(){ | ||
|
||
} | ||
|
||
public func respond(to request: Request, chainingTo next: AsyncResponder) async throws -> Response { | ||
guard let token = request.headers.bearerAuthorization else { | ||
throw Abort(.unauthorized, reason: "No access token found in header") | ||
} | ||
do { | ||
try request.jwt.verify(token.token, as: JWTVerificationPayload.self) | ||
|
||
} catch let error as JWTError { | ||
throw Abort(.unauthorized, reason: error.reason) | ||
} | ||
return try await next.respond(to: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Qiwei Li on 7/18/22. | ||
// | ||
|
||
import Foundation | ||
import JWT | ||
|
||
public struct JWTVerificationPayload: JWTPayload { | ||
// Maps the longer Swift property names to the | ||
// shortened keys used in the JWT payload. | ||
enum CodingKeys: String, CodingKey { | ||
case subject = "sub" | ||
case expiration = "exp" | ||
case userId = "userId" | ||
} | ||
|
||
// The "sub" (subject) claim identifies the principal that is the | ||
// subject of the JWT. | ||
var subject: SubjectClaim | ||
|
||
var expiration: ExpirationClaim | ||
|
||
// UserId | ||
var userId: String | ||
|
||
public func verify(using signer: JWTSigner) throws { | ||
try self.expiration.verifyNotExpired() | ||
} | ||
} |
43 changes: 0 additions & 43 deletions
43
packages/common/Sources/common/Utils/InitializeHealper.swift
This file was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
packages/common/Sources/common/Utils/InitializeHelper.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,64 @@ | ||
import Vapor | ||
import Fluent | ||
import SotoS3 | ||
import FluentMongoDriver | ||
import MQTTNIO | ||
import JWT | ||
|
||
|
||
public class InitializeHelper { | ||
let app: Application | ||
|
||
public init(app: Application) { | ||
self.app = app | ||
} | ||
|
||
// configures your application | ||
public func initializeDB() throws { | ||
try app.databases.use(.mongo( | ||
connectionString: Environment.get(ENVIRONMENT_DB_KEY)! | ||
), as: .mongo) | ||
} | ||
|
||
public func initializeAWS() throws { | ||
let accessKey = Environment.get(ENVIRONMENT_ACCESS_KEY)! | ||
let secretKey = Environment.get(ENVIRONMENT_SECRET_KEY)! | ||
let endpoint = Environment.get(ENVIRONMENT_S3_ENDPOINT)! | ||
let region = Environment.get(ENVIRONMENT_S3_REGION)! | ||
|
||
var configuration = HTTPClient.Configuration() | ||
configuration.httpVersion = .http1Only | ||
|
||
app.aws.client = AWSClient( | ||
credentialProvider: .static(accessKeyId: accessKey, secretAccessKey: secretKey), | ||
httpClientProvider: .shared(HTTPClient( | ||
eventLoopGroupProvider: .createNew, | ||
configuration: configuration | ||
)) | ||
) | ||
app.aws.s3 = S3(client: app.aws.client, region: .other(region), endpoint: endpoint) | ||
} | ||
|
||
public func initializeMQTT() throws { | ||
let client = MQTTClient( | ||
host: "localhost", | ||
port: 1883, | ||
identifier: "Client", | ||
eventLoopGroupProvider: .createNew, | ||
configuration: .init(userName: "user", password: "password") | ||
) | ||
_ = try client.connect().wait() | ||
app.mqtt.client = client | ||
} | ||
|
||
public func initializeJWT() throws { | ||
app.jwt.signers.use(.hs256(key: Environment.get(ENVIRONMENT_PASSWORD)!)) | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.