项目作者: etaty

项目描述 :
JWT (Json Web Token) Scala library
高级语言: Scala
项目地址: git://github.com/etaty/jwtyped.git
创建时间: 2016-11-05T14:37:36Z
项目社区:https://github.com/etaty/jwtyped

开源协议:Apache License 2.0

下载


JWTyped

Build Status codecov Maven Central

Opinionated implementation of JWT in scala

  • secure by default (only secure algorithm supported, no none)
  • small api enforcing security
  • typed
  • no external dependencies

Getting Started

Dependencies

  1. libraryDependencies += "com.github.etaty" %% "jwtyped" % "0.1.0"

Usage

  1. // encode
  2. val key = Secret.fromString("secret")
  3. val algorithm = HS256(key)
  4. val header = Header("JWT", "HS256")
  5. val payload = Payload(sub = "1234567890", name = "John Doe", admin = true)
  6. val message = Message.from(header, payload)
  7. val tokenEncoded = JWT.encode(message, algorithm)
  8. // decode
  9. JWT.decode[Header, Payload](tokenEncoded, algorithm)
  10. // or
  11. JWT.decode[Header, Payload](tokenEncoded, {
  12. case (Header, Payload) =>
  13. // you can decide which algorithm you want to use
  14. Right(algorithm)
  15. })

Algorithms supported

see implemation file

  • HmacSHA*
    • HS256
    • HS384
    • HS512
  • SHA*withRSA
    • RS256
    • RS384
    • RS512
  • SHA*withECDSA
    • ES256
    • ES384
    • ES512

SHA*withECDSA with Bouncy Castle

Dependencies

  1. libraryDependencies += "org.bouncycastle" % "bcpkix-jdk15on" % "1.55"

Add bouncy castle as a provider

  1. import java.security.Security
  2. import org.bouncycastle.jce.provider.BouncyCastleProvider
  3. val BOUNCY_CASTLE_PROVIDER = "BC"
  4. if (Security.getProvider(BOUNCY_CASTLE_PROVIDER) == null) {
  5. Security.addProvider(new BouncyCastleProvider())
  6. }