-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6efb0d0
commit da1fe1d
Showing
5 changed files
with
118 additions
and
0 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,6 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<startup> | ||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | ||
</startup> | ||
</configuration> |
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,78 @@ | ||
module JWTSample | ||
open System | ||
open Saturn.Application | ||
open Saturn.ControllerHelpers | ||
open System.Security.Claims | ||
open System.IdentityModel.Tokens.Jwt | ||
open Microsoft.IdentityModel.Tokens | ||
open Saturn.Router | ||
open Giraffe | ||
open Microsoft.AspNetCore.Http | ||
open Saturn.Pipeline | ||
|
||
//Based on https://medium.com/@dsincl12/json-web-token-with-giraffe-and-f-4cebe1c3ef3b | ||
|
||
let secret = "spadR2dre#u-ruBrE@TepA&*Uf@U" | ||
let issuer = "saturnframework.io" | ||
|
||
[<CLIMutable>] | ||
type LoginViewModel = | ||
{ | ||
Email : string | ||
Password : string | ||
} | ||
|
||
[<CLIMutable>] | ||
type TokenResult = | ||
{ | ||
Token : string | ||
} | ||
|
||
let generateToken email = | ||
let claims = [| | ||
Claim(JwtRegisteredClaimNames.Sub, email); | ||
Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) |] | ||
claims | ||
|> Authentication.generateToken (secret, SecurityAlgorithms.HmacSha256) issuer (DateTime.UtcNow.AddHours(1.0)) | ||
|
||
let handleGetSecured = | ||
fun (next : HttpFunc) (ctx : HttpContext) -> | ||
let email = ctx.User.FindFirst ClaimTypes.NameIdentifier | ||
text ("User " + email.Value + " is authorized to access this resource.") next ctx | ||
|
||
let handlePostToken = | ||
fun (next : HttpFunc) (ctx : HttpContext) -> | ||
task { | ||
let! model = ctx.BindJsonAsync<LoginViewModel>() | ||
|
||
// authenticate user | ||
|
||
let tokenResult = generateToken model.Email | ||
|
||
return! json tokenResult next ctx | ||
} | ||
|
||
let securedRouter = scope { | ||
pipe_through jwtAuthentication | ||
get "/" handleGetSecured | ||
} | ||
|
||
let topRouter = scope { | ||
error_handler (setStatusCode 404 >=> text "Not Found") | ||
|
||
post "/token" handlePostToken | ||
get "/" (text "public route") | ||
forward "/secured" securedRouter | ||
} | ||
|
||
let app = application { | ||
use_jwt_authentication secret issuer | ||
|
||
router topRouter | ||
url "http://0.0.0.0:8085/" | ||
} | ||
|
||
[<EntryPoint>] | ||
let main _ = | ||
run app | ||
0 |
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net461</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="../../src/Saturn/Saturn.fsproj"> | ||
<Name>Saturn.fsproj</Name> | ||
</ProjectReference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="JWTSample.fs" /> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<Import Project="..\..\.paket\Paket.Restore.targets" /> | ||
</Project> |
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,3 @@ | ||
FSharp.Core | ||
Giraffe | ||
Microsoft.AspNetCore |