Auth works! (#1)

This commit is contained in:
Daniel J. Summers 2020-11-23 20:01:50 -05:00
parent d2b5ddf034
commit 6d16bef137
5 changed files with 15 additions and 23 deletions

View File

@ -1,2 +1,3 @@
obj/ obj/
bin/ bin/
appsettings.*.json

View File

@ -1,31 +1,20 @@
module JobsJobsJobs.Api.Auth module JobsJobsJobs.Api.Auth
open FSharp.Json
open JWT
/// A JWT (de)serializer utilizing FSharp.Json
type FSharpJsonSerializer () =
interface IJsonSerializer with
member __.Serialize (any : obj) =
Json.serialize any
member __.Deserialize<'T> json =
Json.deserialize<'T> json
open Data open Data
open Domain open Domain
open FSharp.Json
open JWT.Algorithms open JWT.Algorithms
open JWT.Builder open JWT.Builder
open JWT.Exceptions
open System open System
open System.Net.Http open System.Net.Http
open System.Net.Http.Headers open System.Net.Http.Headers
open JWT.Exceptions
/// Verify a user's credentials with No Agenda Social /// Verify a user's credentials with No Agenda Social
let verifyWithMastodon accessToken = async { let verifyWithMastodon accessToken = async {
use client = new HttpClient () use client = new HttpClient ()
use req = new HttpRequestMessage (HttpMethod.Get, $"{config.auth.apiUrl}accounts/verify_credentials") use req = new HttpRequestMessage (HttpMethod.Get, $"{config.auth.apiUrl}accounts/verify_credentials")
req.Headers.Authorization <- AuthenticationHeaderValue $"Bearer {accessToken}" req.Headers.Authorization <- AuthenticationHeaderValue ("Bearer", accessToken)
match! client.SendAsync req |> Async.AwaitTask with match! client.SendAsync req |> Async.AwaitTask with
| res when res.IsSuccessStatusCode -> | res when res.IsSuccessStatusCode ->
let! body = res.Content.ReadAsStringAsync () let! body = res.Content.ReadAsStringAsync ()
@ -45,7 +34,6 @@ let createJwt citizenId = async {
.WithAlgorithm(HMACSHA256Algorithm ()) .WithAlgorithm(HMACSHA256Algorithm ())
// TODO: generate separate secret for server // TODO: generate separate secret for server
.WithSecret(config.auth.secret) .WithSecret(config.auth.secret)
.WithSerializer(FSharpJsonSerializer ())
.AddClaim("sub", CitizenId.toString citizen.id) .AddClaim("sub", CitizenId.toString citizen.id)
.AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1.).ToUnixTimeSeconds ()) .AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1.).ToUnixTimeSeconds ())
.AddClaim("nam", citizen.displayName) .AddClaim("nam", citizen.displayName)

View File

@ -53,10 +53,12 @@ module Error =
let error (ex : Exception) msg = let error (ex : Exception) msg =
fun ctx -> fun ctx ->
seq { seq {
string ctx.request.url yield string ctx.request.url
match msg with "" -> () | _ -> " ~ "; msg match msg with
"\n"; (ex.GetType().Name); ": "; ex.Message; "\n" | "" -> ()
ex.StackTrace | _ -> yield " ~ "; yield msg
yield "\n"; yield (ex.GetType().Name); yield ": "; yield ex.Message; yield "\n"
yield ex.StackTrace
} }
|> Seq.reduce (+) |> Seq.reduce (+)
|> (eventX >> ctx.runtime.logger.error) |> (eventX >> ctx.runtime.logger.error)
@ -116,9 +118,7 @@ module Citizen =
| Error exn -> return! Error.error exn "Could not issue access token" ctx | Error exn -> return! Error.error exn "Could not issue access token" ctx
| Error exn -> return! Error.error exn "Could not update Jobs, Jobs, Jobs database" ctx | Error exn -> return! Error.error exn "Could not update Jobs, Jobs, Jobs database" ctx
| Error exn -> return! Error.error exn "Token not received" ctx | Error exn -> return! Error.error exn "Token not received" ctx
| Error msg -> | Error msg -> return! Error.error (exn msg) "Could not authenticate with NAS" ctx
// Error message regarding exclusivity to No Agenda Social members
return Some ctx
| Error exn -> return! Error.error exn "Token not received" ctx | Error exn -> return! Error.error exn "Token not received" ctx
} }

View File

@ -0,0 +1,3 @@
{
}

View File

@ -53,7 +53,7 @@ export async function logOn(authCode: string) {
}) })
) )
const token = await resp.json() const token = await resp.json()
await jjjAuthorize(token.access_code) await jjjAuthorize(token.access_token)
// TODO: navigate to user welcome page // TODO: navigate to user welcome page
console.info(`Success - response ${JSON.stringify(token)}`) console.info(`Success - response ${JSON.stringify(token)}`)
} catch (e) { } catch (e) {