Skip to content

Instantly share code, notes, and snippets.

@whitetigle
Last active October 9, 2018 09:40
Show Gist options
  • Save whitetigle/4da99eca2cb4bdf9941cffc0cab6e09f to your computer and use it in GitHub Desktop.
Save whitetigle/4da99eca2cb4bdf9941cffc0cab6e09f to your computer and use it in GitHub Desktop.
// ts2fable 0.6.0-build.320
module rec Fable.Import.JsonWebToken
open System
open Fable.Core
//open Fable.Import.JS
[<Import("default", from="JsonWebToken")>]
let JsonWebToken: IExports = jsNative
type [<AllowNullLiteral>] IExports =
abstract JsonWebTokenError: JsonWebTokenErrorStatic
abstract TokenExpiredError: TokenExpiredErrorStatic
abstract NotBeforeError: NotBeforeErrorStatic
/// <summary>Synchronously sign the given payload into a JSON Web Token string</summary>
/// <param name="payload">- Payload to sign, could be an literal, buffer or string</param>
/// <param name="secretOrPrivateKey">- Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.</param>
/// <param name="options">- Options for the signature</param>
abstract sign: payload: U3<string, Buffer, obj> * secretOrPrivateKey: Secret * ?options: SignOptions -> string
/// <summary>Sign the given payload into a JSON Web Token string</summary>
/// <param name="payload">- Payload to sign, could be an literal, buffer or string</param>
/// <param name="secretOrPrivateKey">- Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.</param>
/// <param name="callback">- Callback to get the encoded token on</param>
abstract sign: payload: U3<string, Buffer, obj> * secretOrPrivateKey: Secret * callback: SignCallback -> unit
abstract sign: payload: U3<string, Buffer, obj> * secretOrPrivateKey: Secret * options: SignOptions * callback: SignCallback -> unit
/// <summary>Synchronously verify given token using a secret or a public key to get a decoded token</summary>
/// <param name="token">- JWT string to verify</param>
/// <param name="secretOrPublicKey">- Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.</param>
abstract verify: token: string * secretOrPublicKey: U2<string, Buffer> -> U2<obj, string>
abstract verify: token: string * secretOrPublicKey: U2<string, Buffer> * ?options: VerifyOptions -> U2<obj, string>
/// <summary>Asynchronously verify given token using a secret or a public key to get a decoded token</summary>
/// <param name="token">- JWT string to verify</param>
/// <param name="secretOrPublicKey">- Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.</param>
/// <param name="callback">- Callback to get the decoded token on</param>
abstract verify: token: string * secretOrPublicKey: U2<string, Buffer> * ?callback: VerifyCallback -> unit
abstract verify: token: string * secretOrPublicKey: U2<string, Buffer> * ?options: VerifyOptions * ?callback: VerifyCallback -> unit
/// <summary>Returns the decoded payload without verifying if the signature is valid.</summary>
/// <param name="token">- JWT string to decode</param>
/// <param name="options">- Options for decoding</param>
abstract decode: token: string * ?options: DecodeOptions -> U2<obj, string> option
type [<AllowNullLiteral>] JsonWebTokenError =
inherit Error
abstract inner: Error with get, set
type [<AllowNullLiteral>] JsonWebTokenErrorStatic =
[<Emit "new $0($1...)">] abstract Create: message: string * ?error: Error -> JsonWebTokenError
type [<AllowNullLiteral>] TokenExpiredError =
inherit JsonWebTokenError
abstract expiredAt: float with get, set
type [<AllowNullLiteral>] TokenExpiredErrorStatic =
[<Emit "new $0($1...)">] abstract Create: message: string * expiredAt: float -> TokenExpiredError
type [<AllowNullLiteral>] NotBeforeError =
inherit JsonWebTokenError
abstract date: DateTime with get, set
type [<AllowNullLiteral>] NotBeforeErrorStatic =
[<Emit "new $0($1...)">] abstract Create: message: string * date: DateTime -> NotBeforeError
type [<AllowNullLiteral>] SignOptions =
/// Signature algorithm. Could be one of these values :
/// - HS256: HMAC using SHA-256 hash algorithm (default)
/// - HS384: HMAC using SHA-384 hash algorithm
/// - HS512: HMAC using SHA-512 hash algorithm
/// - RS256: RSASSA using SHA-256 hash algorithm
/// - RS384: RSASSA using SHA-384 hash algorithm
/// - RS512: RSASSA using SHA-512 hash algorithm
/// - ES256: ECDSA using P-256 curve and SHA-256 hash algorithm
/// - ES384: ECDSA using P-384 curve and SHA-384 hash algorithm
/// - ES512: ECDSA using P-521 curve and SHA-512 hash algorithm
/// - none: No digital signature or MAC value included
abstract algorithm: string option with get, set
abstract keyid: string option with get, set
abstract expiresIn: U2<string, float> option with get, set
abstract notBefore: U2<string, float> option with get, set
abstract audience: U2<string, ResizeArray<string>> option with get, set
abstract subject: string option with get, set
abstract issuer: string option with get, set
abstract jwtid: string option with get, set
abstract noTimestamp: bool option with get, set
abstract header: obj option with get, set
abstract encoding: string option with get, set
type [<AllowNullLiteral>] VerifyOptions =
abstract algorithms: ResizeArray<string> option with get, set
abstract audience: U2<string, ResizeArray<string>> option with get, set
abstract clockTimestamp: float option with get, set
abstract clockTolerance: float option with get, set
abstract issuer: U2<string, ResizeArray<string>> option with get, set
abstract ignoreExpiration: bool option with get, set
abstract ignoreNotBefore: bool option with get, set
abstract jwtid: string option with get, set
abstract subject: string option with get, set
abstract maxAge: string option with get, set
type [<AllowNullLiteral>] DecodeOptions =
abstract complete: bool option with get, set
abstract json: bool option with get, set
type VerifyErrors =
U3<JsonWebTokenError, NotBeforeError, TokenExpiredError>
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module VerifyErrors =
let ofJsonWebTokenError v: VerifyErrors = v |> U3.Case1
let isJsonWebTokenError (v: VerifyErrors) = match v with U3.Case1 _ -> true | _ -> false
let asJsonWebTokenError (v: VerifyErrors) = match v with U3.Case1 o -> Some o | _ -> None
let ofNotBeforeError v: VerifyErrors = v |> U3.Case2
let isNotBeforeError (v: VerifyErrors) = match v with U3.Case2 _ -> true | _ -> false
let asNotBeforeError (v: VerifyErrors) = match v with U3.Case2 o -> Some o | _ -> None
let ofTokenExpiredError v: VerifyErrors = v |> U3.Case3
let isTokenExpiredError (v: VerifyErrors) = match v with U3.Case3 _ -> true | _ -> false
let asTokenExpiredError (v: VerifyErrors) = match v with U3.Case3 o -> Some o | _ -> None
type [<AllowNullLiteral>] VerifyCallback =
[<Emit "$0($1...)">] abstract Invoke: err: VerifyErrors * decoded: U2<obj, string> -> unit
type [<AllowNullLiteral>] SignCallback =
[<Emit "$0($1...)">] abstract Invoke: err: Error * encoded: string -> unit
type Secret =
U4<string, Buffer, obj, obj>
[<RequireQualifiedAccess; CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Secret =
let ofString v: Secret = v |> U4.Case1
let isString (v: Secret) = match v with U4.Case1 _ -> true | _ -> false
let asString (v: Secret) = match v with U4.Case1 o -> Some o | _ -> None
let ofBuffer v: Secret = v |> U4.Case2
let isBuffer (v: Secret) = match v with U4.Case2 _ -> true | _ -> false
let asBuffer (v: Secret) = match v with U4.Case2 o -> Some o | _ -> None
let ofKey v: Secret = v |> U4.Case3
let isKey (v: Secret) = match v with U4.Case3 _ -> true | _ -> false
let asKey (v: Secret) = match v with U4.Case3 o -> Some o | _ -> None
let ofPassphrase v: Secret = v |> U4.Case4
let isPassphrase (v: Secret) = match v with U4.Case4 _ -> true | _ -> false
let asPassphrase (v: Secret) = match v with U4.Case4 o -> Some o | _ -> None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment