{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Libjwt.Algorithms
( Algorithm(..)
, RsaKey
, EcKey
, toHeaderAlg
, jwtAlgWithKey
)
where
import Libjwt.Header ( Alg(..) )
import Libjwt.FFI.Libjwt
import Libjwt.Keys
import Data.Kind ( Constraint )
import GHC.TypeLits
data Algorithm k where
HMAC256 ::Secret -> Algorithm Secret
HMAC384 ::Secret -> Algorithm Secret
HMAC512 ::Secret -> Algorithm Secret
RSA256 ::RsaKey r => r -> Algorithm r
RSA384 ::RsaKey r => r -> Algorithm r
RSA512 ::RsaKey r => r -> Algorithm r
ECDSA256 ::EcKey e => e -> Algorithm e
ECDSA384 ::EcKey e => e -> Algorithm e
ECDSA512 ::EcKey e => e -> Algorithm e
AlgNone ::Algorithm ()
deriving stock instance Show k => Show (Algorithm k)
type family RsaKey t :: Constraint where
RsaKey RsaKeyPair = ()
RsaKey RsaPubKey = ()
RsaKey a = TypeError ('Text "RSASSA-PKCS-v1_5 cannot be used with " ':<>: 'ShowType a)
type family EcKey t :: Constraint where
EcKey EcKeyPair = ()
EcKey EcPubKey = ()
EcKey a = TypeError ('Text "ECDSA cannot be used with " ':<>: 'ShowType a)
jwtAlgWithKey :: Algorithm k -> (JwtAlgT, k)
jwtAlgWithKey :: Algorithm k -> (JwtAlgT, k)
jwtAlgWithKey (HMAC256 Secret
secret) = (JwtAlgT
jwtAlgHs256, k
Secret
secret)
jwtAlgWithKey (HMAC384 Secret
secret) = (JwtAlgT
jwtAlgHs384, k
Secret
secret)
jwtAlgWithKey (HMAC512 Secret
secret) = (JwtAlgT
jwtAlgHs512, k
Secret
secret)
jwtAlgWithKey (RSA256 k
key ) = (JwtAlgT
jwtAlgRs256, k
key)
jwtAlgWithKey (RSA384 k
key ) = (JwtAlgT
jwtAlgRs384, k
key)
jwtAlgWithKey (RSA512 k
key ) = (JwtAlgT
jwtAlgRs512, k
key)
jwtAlgWithKey (ECDSA256 k
key ) = (JwtAlgT
jwtAlgEs256, k
key)
jwtAlgWithKey (ECDSA384 k
key ) = (JwtAlgT
jwtAlgEs384, k
key)
jwtAlgWithKey (ECDSA512 k
key ) = (JwtAlgT
jwtAlgEs512, k
key)
jwtAlgWithKey Algorithm k
AlgNone = (JwtAlgT
jwtAlgNone, ())
toHeaderAlg :: Algorithm k -> Alg
(HMAC256 Secret
_) = Alg
HS256
toHeaderAlg (HMAC384 Secret
_) = Alg
HS384
toHeaderAlg (HMAC512 Secret
_) = Alg
HS512
toHeaderAlg (RSA256 k
_) = Alg
RS256
toHeaderAlg (RSA384 k
_) = Alg
RS384
toHeaderAlg (RSA512 k
_) = Alg
RS512
toHeaderAlg (ECDSA256 k
_) = Alg
ES256
toHeaderAlg (ECDSA384 k
_) = Alg
ES384
toHeaderAlg (ECDSA512 k
_) = Alg
ES512
toHeaderAlg Algorithm k
AlgNone = Alg
None