Skip to content

Commit

Permalink
doc updates using source code
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt committed Mar 10, 2021
1 parent 5d52013 commit b25fb24
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 31 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ scala:
dist: xenial

script:
- ./mill --disable-ticker __.compile
- ./mill --disable-ticker __.test
# - mill --disable-ticker jose.json._.test

Expand Down
44 changes: 14 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<script type='module' src="https://kag0.github.io/sauce/sauce.js">
<h1>If you're reading this, click <a href="https://blackdoor.github.io/jose">HERE</a></h1>
</script>
# jose
[![](https://img.shields.io/codacy/grade/177db012dc7548be9143a7562cd1d4bd.svg?style=flat-square)](https://app.codacy.com/project/blackdoor/jose/dashboard)
[![Travis (.com)](https://img.shields.io/travis/com/blackdoor/jose.svg?style=flat-square)](https://travis-ci.com/blackdoor/jose)
Expand All @@ -16,25 +20,11 @@ The dependency is available on [Maven Central](https://mvnrepository.com/artifac
Pretty simple: make a key, make something to sign, sign it.
```scala
val key = P256KeyPair.generate
val claims = Claims(
sub = Some("my user"),
iss = Some("me"),
exp = Some(Instant.now.plus(1, ChronoUnit.DAYS))
)

val token = Jwt.sign(
claims,
key.withAlg(Some("ES256"))
)

val errorOrJwt = Jwt
.validate(token)
.using(key, Check.iss(_ == "me"))
.now
errorOrJwt.right.get.claims.sub // Some(my user)
```
<sauce-code
repo='blackdoor/jose'
file='docs/src/black/door/jose/docs/SampleCode.scala'
lines='15:36'
></sauce-code>
### Selecting a JSON implementation
Expand Down Expand Up @@ -77,17 +67,11 @@ Jwt
So for example you could synchronously validate a JWT with some custom claims with
```scala
val customValidator =
JwtValidator.fromSync[MyCustomClaimsClass] {
case jwt if !jwt.claims.unregistered.thisTokenIsForAnAdmin =>
"Token needs to be for an admin"
}

Jwt.validate(compact)[MyCustomClaimsClass]
.using(es256Key, customValidator)
.now
```
<sauce-code
repo='blackdoor/jose'
file='docs/src/black/door/jose/docs/SampleCode.scala'
lines='64:76'
></sauce-code>
> Not yet implemented:
> * JWK serialization partly implemented
Expand Down
7 changes: 6 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ trait BaseModule extends CrossScalaModule {
}

object jose extends Cross[JoseModule](`2.12`, `2.13`)
class JoseModule(val crossScalaVersion: String) extends BaseModule with PublishModule {
class JoseModule(val crossScalaVersion: String) extends BaseModule with PublishModule {

def ivyDeps = Agg(
ivy"org.typelevel::cats-core:2.2.0",
Expand Down Expand Up @@ -81,3 +81,8 @@ object json extends Module {
def moduleDeps = List(jose(crossScalaVersion))
}
}

object docs extends ScalaModule {
def scalaVersion = `2.13`
def moduleDeps = List(json.ninny(`2.13`))
}
81 changes: 81 additions & 0 deletions docs/src/black/door/jose/docs/SampleCode.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package black.door.jose.docs

import java.time.Instant
import java.time.temporal.ChronoUnit
import black.door.jose.json.ninny.JsonSupport._
import io.github.kag0.ninny.Json

object SampleCode extends App {

implicit def autoSome[A](a: A) = Some(a)

// format: off
{

import black.door.jose.jwk._
import black.door.jose.jwt._

val key = P256KeyPair.generate

val claims = Claims(
sub = "my user",
iss = "me",
exp = Instant.now.plus(1, ChronoUnit.DAYS)
)

val token = Jwt.sign(
claims,
key.withAlg("ES256")
)

val errorOrJwt = Jwt
.validate(token)
.using(key, Check.iss(_ == "me"))
.now

errorOrJwt.right.get.claims.sub // Some(my user)

} // format: on

{
import black.door.jose.jwk._
import io.github.kag0.ninny.Auto._
case class MyCustomClaimsClass(thisTokenIsForAnAdmin: Boolean)
val es256Key = Json
.parse(
"""
|{
| "kty": "EC",
| "d": "L5qVktaHtMwgo9RrDGw7pAsUlUpGsaLnIXwvpJNba_I",
| "use": "sig",
| "crv": "P-256",
| "x": "ZalefywgnJq0iuzt8HFIX4hK6LHqkOX6_IV7idd-8c0",
| "y": "8cAfsN9JW7X9ukqystF198wdv6JovCG99qfB-LUjblo",
| "alg": "ES256"
|}""".stripMargin
)
.to[Jwk]
.get

val compact =
"eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJ0aGlzVG9rZW5Jc0ZvckFuQWRtaW4iOmZhbHNlfQ.OEpXwMiFjOIk-vSjBekB67m2cwwIAd6vyVkIP5FgRS8L4MBc6q7qdpSZwkTkkg7oSv5G6YI5UIJUIgnjq_5Jdg"

// format: off

import black.door.jose.jwt._

val customValidator =
JwtValidator.fromSync[MyCustomClaimsClass] {
case jwt if !jwt.claims.unregistered.thisTokenIsForAnAdmin =>
"Token needs to be for an admin"
}

Jwt
.validate(compact)[MyCustomClaimsClass]
.using(es256Key, customValidator)
.now
// Left(Token needs to be for an admin)

// format: on
}
}

0 comments on commit b25fb24

Please sign in to comment.