Skip to content
This repository has been archived by the owner on Nov 20, 2021. It is now read-only.

ulasakdeniz/akka-http-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

akka-http-utils Build Status license Coverage Status

Utilities for akka-http

auth subproject consists of akka-http oauth client implementation with no extra dependency.

The only way to use it for now is to build and publish locally with sbt publishLocal command. Then it can be added to a project with the following dependency:

"com.ulasakdeniz.akka-http-utils" %% "auth" % "0.2.0-SNAPSHOT"

Below there is an example of using OAuth1Directives for Twitter OAuth Authentication.

import akka.actor.ActorSystem
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.stream.ActorMaterializer
import com.ulasakdeniz.auth.oauth1._

trait Api extends OAuth1Directives {
  implicit val system: ActorSystem
  implicit val mat: ActorMaterializer

  val twitterOAuthParams: OAuthParams = OAuthParams(
    consumerKey = "FILL",
    consumerSecret = "FILL",
    requestTokenUri = "https://api.twitter.com/oauth/request_token",
    accessTokenUri = "https://api.twitter.com/oauth/access_token",
    authorizationUri = "https://api.twitter.com/oauth/authenticate")

  override val oauthContext: OAuthContext = OAuthContext(twitterOAuthParams)

  // access token cache
  var tokenCache: Map[String, Tokens] = Map.empty

  // temporary token cache
  var temporaryCache: Map[String, Tokens] = Map.empty

  val routes: Route =
    path("auth") {
      get {
        oauth {
          case RedirectionSuccess(httpResponse, tokens) =>
            temporaryCache = temporaryCache + (tokens("oauth_token") -> tokens)
            // Redirects user to get Access Token. Then the service provider (Twitter) will make a request to callback endpoint.
            complete(httpResponse)

          case RequestTokenFailed(_) =>
            complete(StatusCodes.ImATeapot)
        }
      }
    } ~
      // Note that this endpoint needs to be registered to service provider as callback url.
      pathPrefix("callback") {
        val tokenProvider: String => Map[String, String] = oauthToken => temporaryCache(oauthToken)
        oauthCallback(tokenProvider) {
          case AccessTokenSuccess(tokens) =>
            // Twitter gives "screen_name" as additional parameter in tokens.
            tokenCache = tokenCache + (tokens("screen_name") -> tokens)
            complete("Received access tokens for user1.")

          case AccessTokenFailed(_) =>
            complete(StatusCodes.ImATeapot)
        }
      }
}