Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Created July 28, 2012 23:54
Show Gist options
  • Save ahoy-jon/3195331 to your computer and use it in GitHub Desktop.
Save ahoy-jon/3195331 to your computer and use it in GitHub Desktop.
package test
import scalaz._
import Scalaz._
trait Claim[+S] {
protected val statement: S
def verify(implicit fn: S=> Boolean ): Option[S]
}
object Claim {
implicit val ClaimMonad = new Monad[Claim] with Traverse[Claim] {
def traverseImpl[G[_] : Applicative, A, B](fa: Claim[A])(f: A => G[B]): G[Claim[B]] = f(fa.statement).map(a => this.point(a))
def point[A](a: => A) = new Claim[A]{
protected val statement : A = a;
def verify(implicit fn: A=> Boolean ): Option[A] = fn(statement) match {
case true => Some(statement)
case false => None
}
}
def bind[A, B](fa: Claim[A])(f: (A) => Claim[B]) = f(fa.statement)
}
}
import java.security.cert.X509Certificate
import java.security.PublicKey
object test {
def process( x509claim: Claim[X509Certificate]) {
val webidClaims = for (x509 <- x509claim) yield {
Option(x509.getSubjectAlternativeNames()).toList.flatMap { coll =>
import scala.collection.JavaConverters.iterableAsScalaIterableConverter
for {
sanPair <- coll.asScala if (sanPair.get(0) == 6)
} yield (sanPair.get(1).asInstanceOf[String].trim,x509.getPublicKey)
}
}
val res: List[Claim[Pair[String,PublicKey]]] = webidClaims.sequence
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment