1
package hammock
2
package hi
3

4
import java.util.Base64
5
import cats._
6
import cats.implicits._
7

8
sealed trait Auth
9
object Auth {
10
  case class BasicAuth(user: String, pass: String) extends Auth
11
  case class OAuth2Bearer(token: String)           extends Auth
12
  case class OAuth2Token(token: String)            extends Auth
13

14 1
  implicit val authShow = new Show[Auth] {
15
    def show(a: Auth): String = a match {
16
      case BasicAuth(user, pass) =>
17 1
        val toEncode = s"$user:$pass".getBytes
18 1
        val encoded  = Base64.getEncoder.encode(toEncode)
19 1
        s"Basic ${new String(encoded)}"
20 1
      case OAuth2Bearer(token) => s"Bearer $token"
21 1
      case OAuth2Token(token)  => s"token $token"
22
    }
23
  }
24

25 1
  implicit val authEq = new Eq[Auth] {
26
    def eqv(a: Auth, b: Auth): Boolean = (a, b) match {
27 1
      case (BasicAuth(u1, p1), BasicAuth(u2, p2)) => u1 === u2 && p1 === p2
28 1
      case (OAuth2Bearer(t1), OAuth2Bearer(t2)) => t1 === t2
29 1
      case (OAuth2Token(t1), OAuth2Token(t2)) => t1 === t2
30 0
      case _ => false
31
    }
32
  }
33
}

Read our documentation on viewing source code .

Loading