Skip to content

Commit 6efb0d0

Browse files
Add helpers for JWT authentication
1 parent b5b6235 commit 6efb0d0

File tree

6 files changed

+229
-60
lines changed

6 files changed

+229
-60
lines changed

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ nuget Microsoft.AspNetCore.StaticFiles
99
nuget Microsoft.Extensions.Caching.Memory
1010
nuget Microsoft.AspNetCore.Rewrite
1111
nuget Microsoft.AspNetCore.Cors
12+
nuget Microsoft.AspNetCore.Authentication.JwtBearer
1213

1314
nuget Giraffe
1415

paket.lock

Lines changed: 178 additions & 55 deletions
Large diffs are not rendered by default.

src/Saturn/Application.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ open Microsoft.Extensions.Logging
1111
open System.IO
1212
open Microsoft.AspNetCore.Rewrite
1313
open Microsoft.AspNetCore.Cors.Infrastructure
14+
open Microsoft.AspNetCore.Authentication.JwtBearer
15+
open Microsoft.IdentityModel.Tokens
1416

1517
type ApplicationState = {
1618
Router: HttpHandler option
@@ -149,6 +151,30 @@ module Application =
149151
ServicesConfig = service::state.ServicesConfig
150152
AppConfigs = middleware::state.AppConfigs
151153
}
154+
[<CustomOperation("use_jwt_authentication")>]
155+
member __.UseJWTAuth(state: ApplicationState, secret: string, issuer : string) =
156+
let middleware (app : IApplicationBuilder) =
157+
app.UseAuthentication()
158+
159+
let service (s : IServiceCollection) =
160+
s.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
161+
.AddJwtBearer(fun opt ->
162+
let tvp = TokenValidationParameters()
163+
tvp.ValidateActor <- true
164+
tvp.ValidateAudience <- true
165+
tvp.ValidateLifetime <- true
166+
tvp.ValidateIssuerSigningKey <- true
167+
tvp.ValidIssuer <- issuer
168+
tvp.ValidAudience <- issuer
169+
tvp.IssuerSigningKey <- SymmetricSecurityKey(Text.Encoding.UTF8.GetBytes secret)
170+
opt.TokenValidationParameters <- tvp
171+
) |> ignore
172+
s
173+
174+
{ state with
175+
ServicesConfig = service::state.ServicesConfig
176+
AppConfigs = middleware::state.AppConfigs
177+
}
152178

153179
let application = ApplicationBuilder()
154180

src/Saturn/ControllerHelpers.fs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Giraffe.Tasks
66
open Giraffe.HttpHandlers
77
open Giraffe.HttpStatusCodeHandlers
88

9+
910
module ControllerHelpers =
1011

1112
[<RequireQualifiedAccess>]
@@ -176,4 +177,21 @@ module ControllerHelpers =
176177
ServerErrors.SERVICE_UNAVAILABLE res (fun c -> task {return Some c}) ctx
177178

178179
let gatewayTimeout (ctx: HttpContext) res =
179-
ServerErrors.GATEWAY_TIMEOUT res (fun c -> task {return Some c}) ctx
180+
ServerErrors.GATEWAY_TIMEOUT res (fun c -> task {return Some c}) ctx
181+
182+
[<RequireQualifiedAccess>]
183+
module Authentication =
184+
open System
185+
open System.Text
186+
open Microsoft.IdentityModel.Tokens
187+
open System.IdentityModel.Tokens.Jwt
188+
189+
190+
let generateToken (secret : string, algorithm) issuer expires claims =
191+
let expires = Nullable(expires)
192+
let notBefore = Nullable(DateTime.UtcNow)
193+
let securityKey = SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret))
194+
let signingCredentials = SigningCredentials(key = securityKey, algorithm = algorithm)
195+
196+
let token = JwtSecurityToken(issuer, issuer, claims, notBefore, expires, signingCredentials )
197+
JwtSecurityTokenHandler().WriteToken token

src/Saturn/Pipelines.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,6 @@ module Pipeline =
195195
ctx.Items.["RequestId"] <- reqId
196196
setHttpHeader "x-request-id" reqId nxt ctx
197197

198-
///TODO: force SSL connections - https://github.com/elixir-plug/plug/blob/v1.4.3/lib/plug/ssl.ex#L1
199-
let ssl : HttpHandler = succeed
200-
198+
///Requires authentication with JWT token using default authentication scheme
199+
let jwtAuthentication : HttpHandler =
200+
requiresAuthentication (challenge Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)

src/Saturn/paket.references

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Microsoft.AspNetCore.StaticFiles
66
Microsoft.AspNetCore.ResponseCompression
77
Microsoft.Extensions.Caching.Memory
88
Microsoft.AspNetCore.Rewrite
9-
Microsoft.AspNetCore.Cors
9+
Microsoft.AspNetCore.Cors
10+
Microsoft.AspNetCore.Authentication.JwtBearer

0 commit comments

Comments
 (0)