-
Notifications
You must be signed in to change notification settings - Fork 2
Flatten unregistered claims #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| module Node.Jwt | ||
| ( module Types | ||
| , decode | ||
| , decode' | ||
| , sign | ||
| , verify | ||
| , verify' | ||
| ) where | ||
|
|
||
| import Types | ||
|
|
@@ -17,10 +19,10 @@ import Data.Newtype (unwrap) | |
| import Data.Options (options, (:=)) | ||
| import Data.Traversable (traverse) | ||
| import Effect.Aff (Aff) | ||
| import Effect.Uncurried (EffectFn3, runEffectFn3) | ||
| import Effect.Uncurried (EffectFn4, runEffectFn4) | ||
| import Foreign (ForeignError, readNullOrUndefined, readString, renderForeignError) | ||
| import Foreign.Generic (F, Foreign) | ||
| import Foreign.Generic (decode) as Generic | ||
| import Foreign.Generic (decode, encode) as Generic | ||
| import Foreign.Index ((!)) | ||
| import Options as Options | ||
| import Prelude (bind, map, pure, ($), (<$>), (<*>), (<>), (>>=)) | ||
|
|
@@ -41,8 +43,8 @@ claims token = | |
| iss <- token ! "payload" ! "iss" >>= readNullOrUndefined >>= traverse readString | ||
| sub <- token ! "payload" ! "sub" >>= readNullOrUndefined >>= traverse readString | ||
| jti <- token ! "payload" ! "jti" >>= readNullOrUndefined >>= traverse readString | ||
| (unregistered :: Maybe (Record r)) <- token ! "payload" ! "unregistered" >>= readNullOrUndefined >>= traverse Generic.decode | ||
| pure { iat, nbf, exp, aud: unwrap <$> aud, iss, sub, jti, unregistered } | ||
| unregisteredClaims <- token ! "payload" ! "unregisteredClaims" >>= readNullOrUndefined >>= traverse Generic.decode | ||
| pure { iat, nbf, exp, aud: unwrap <$> aud, iss, sub, jti, unregisteredClaims } | ||
|
|
||
| -- Extract JWT headers from any foreign value | ||
| headers :: Foreign -> Either (NonEmptyList ForeignError) JOSEHeaders | ||
|
|
@@ -54,42 +56,38 @@ headers token = | |
| cty <- token ! "header" ! "cty" >>= readNullOrUndefined >>= traverse Generic.decode | ||
| pure { alg, typ, kid, cty } | ||
|
|
||
| foreign import _sign :: EffectFn3 Foreign String Foreign (Promise String) | ||
| foreign import _sign :: EffectFn4 Foreign Foreign String Foreign (Promise String) | ||
|
|
||
| sign :: forall r l. Encodable r l => Secret -> JOSEHeaders -> Claims r -> Aff String | ||
| sign (Secret secret) { typ, cty, alg, kid } { iss, sub, aud, exp, nbf, iat, jti, unregistered } = | ||
| sign (Secret secret) { typ, cty, alg, kid } { iss, sub, aud, exp, nbf, iat, jti, unregisteredClaims } = | ||
| toAffE | ||
| $ runEffectFn3 _sign payloadOptions secret | ||
| $ signOptions | ||
| $ runEffectFn4 _sign payloadOptions (Generic.encode unregisteredClaims) secret signOptions | ||
| where | ||
| payloadOptions :: Foreign | ||
| payloadOptions = | ||
| options | ||
| ( (Options.iat := iat) | ||
| <> (Options.nbf := nbf) | ||
| <> (Options.exp := exp) | ||
| <> (Options.unregistered := unregistered) | ||
| ) | ||
| $ (Options.iat := iat) | ||
| <> (Options.nbf := nbf) | ||
| <> (Options.exp := exp) | ||
|
|
||
| signHeaderOptions :: Foreign | ||
| signHeaderOptions = | ||
| options | ||
| $ (Options.typ := typ) | ||
| <> (Options.cty := cty) | ||
| <> (Options.alg := alg) | ||
| <> (Options.kid := kid) | ||
|
|
||
| signOptions :: Foreign | ||
| signOptions = | ||
| options | ||
| ( (Options.algorithm := alg) | ||
| <> (Options.audience := aud) | ||
| <> (Options.issuer := iss) | ||
| <> (Options.jwtid := jti) | ||
| <> (Options.subject := sub) | ||
| <> (Options.keyid := kid) | ||
| <> ( Options.header | ||
| := ( options | ||
| ( (Options.typ := typ) | ||
| <> (Options.cty := cty) | ||
| <> (Options.alg := alg) | ||
| <> (Options.kid := kid) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| $ (Options.algorithm := alg) | ||
| <> (Options.audience := aud) | ||
| <> (Options.issuer := iss) | ||
| <> (Options.jwtid := jti) | ||
| <> (Options.subject := sub) | ||
| <> (Options.keyid := kid) | ||
| <> (Options.header := signHeaderOptions) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cleaned up the options as well |
||
|
|
||
| -- Utility function used to automatically convert any foreign value into a token | ||
| foreignToToken :: forall r l s. Decodable r l => Foreign -> Either (NonEmptyList String) (Token r s) | ||
|
|
@@ -103,7 +101,13 @@ foreign import _decode :: Fn3 (Foreign -> Maybe Foreign) (Maybe Foreign) String | |
| decode :: forall r l. Decodable r l => String -> Either (NonEmptyList String) (Token r Unverified) | ||
| decode s = (note (singleton "Couldn't decode token") $ runFn3 _decode Just Nothing s) >>= foreignToToken | ||
|
|
||
| decode' :: String -> Either (NonEmptyList String) (Token () Unverified) | ||
| decode' = decode | ||
|
|
||
| foreign import _verify :: Fn4 (Foreign -> Maybe Foreign) (Maybe Foreign) String String (Maybe Foreign) | ||
|
|
||
| verify :: forall r l. Decodable r l => Secret -> String -> Either (NonEmptyList String) (Token r Verified) | ||
| verify (Secret secret) s = (note (singleton "Couldn't verify token") $ runFn4 _verify Just Nothing secret s) >>= foreignToToken | ||
| verify (Secret secret) s = (note (singleton "Couldn't verify token") $ runFn4 _verify Just Nothing s secret) >>= foreignToToken | ||
|
|
||
| verify' :: Secret -> String -> Either (NonEmptyList String) (Token () Unverified) | ||
| verify' = verify | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. decode' and verify' are useful when dealing with token that have no unregistered claims, or when the unregistered claims don't matter. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hopefully merged soon, the partitionClaims function is unsafe, dirty, and verbose 😕