Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.36 KB

auth.md

File metadata and controls

50 lines (36 loc) · 1.36 KB

Authentication

--

Authentication with OAuth2

There is finagle-oauth2 server-side provider that is supported in Finch via finch-oauth2 package:

Authorize

import com.twitter.finagle.oauth2._
import io.finch.oauth2._

val dataHandler: DataHandler[Int] = ???
val auth: RequestReader[AuthInfo[Int]] = authorize(dataHandler)
val e: Endpoint[Int] = get("user" ? auth) { ai: AuthInfo[Int] => Ok(ai.user) }

Issue Access Token

import com.twitter.finagle.oauth2._
import io.finch.oauth2._

val token: RequestReader[GrandHandlerResult] = issueAccessToken(dataHandler)
val e: Endpoint[String] = get("token" ? token) { ghr: GrantHandlerResult =>
  Ok(ghr.accessToken)
}

Note that both token and authorize may throw com.twitter.finagle.oauth2.OAuthError that should be explicitly handled and converted into a Output.Failure.

Authentication with Basic HTTP

Basic HTTP Auth is implemented as BasicAuth combinator available in finch-core.

import io.finch._

val basicAuth: BasicAuth = BasicAuth("user", "password")
val e: Endpoint[String] = basicAuth(Endpoint(Ok("secret place")))

-- Read Next: JSON