Files
puregym-unofficial-dotnet/PureGym.App/Authenticate.fs
patrick e96ae78665
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
Use WoofWare.Myriad entirely to generate the REST API (#9)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #9
2024-01-30 00:17:45 +00:00

63 lines
2.0 KiB
Forth

namespace PureGym.App
open System.Threading
open Argu
open System
open PureGym
type GetTokenArg =
| [<Unique ; EqualsAssignmentOrSpaced>] User_Email of string
| [<Unique ; EqualsAssignmentOrSpaced>] Pin of string
| [<Unique>] StdIn
interface IArgParserTemplate with
member s.Usage =
match s with
| GetTokenArg.Pin _ -> "Eight-digit PureGym user's PIN"
| GetTokenArg.User_Email _ -> "PureGym user's email address"
| GetTokenArg.StdIn -> "Read anything not specified on the command line from stdin"
static member Parse (args : ParseResults<GetTokenArg>) : Result<UsernamePin, ArguParseException> =
let canUseStdin = args.TryGetResult(GetTokenArg.StdIn).IsSome
let username =
match args.TryGetResult GetTokenArg.User_Email, canUseStdin with
| None, true ->
Console.Error.Write "Enter username: "
Console.ReadLine ()
| None, false ->
// TODO: proper exception handling, this should be surfaced through the Error
failwith "You must supply --user-email or --stdin"
| Some s, _ -> s
let pin =
match args.TryGetResult GetTokenArg.Pin, canUseStdin with
| None, true ->
Console.Error.Write "Enter eight-digit PIN: "
Console.ReadLine ()
| None, false ->
// TODO: proper exception handling, this should be surfaced through the Error
failwith "You must supply --pin or --stdin"
| Some s, _ -> s
{
Username = username
Pin = pin
}
|> Ok
[<RequireQualifiedAccess>]
module Authenticate =
let run (creds : UsernamePin) =
task {
let! cred = AuthToken.get creds CancellationToken.None
Console.WriteLine cred.AccessToken
match cred.ExpiryTime with
| None -> ()
| Some expiry -> Console.Error.WriteLine $"Expires at {expiry}"
return 0
}