Giter Site home page Giter Site logo

akka-http-oauth2-client's Introduction

akka-http-oauth2-client

Build Status Maven Central

A Scala wrapper for OAuth 2.0 with Akka HTTP.

Getting akka-http-oauth2-client

akka-http-oauth2-client is available in sonatype repository and it targets Akka HTTP 10.0.x. There are scala 2.11 and 2.12 compatible jars available.

libraryDependencies += "com.github.dakatsuka" %% "akka-http-oauth2-client" % "0.1.0"

Usage

import java.net.URI

import akka.actor.ActorSystem
import akka.http.scaladsl.model.Uri
import akka.stream.{ ActorMaterializer, Materializer }
import com.github.dakatsuka.akka.http.oauth2.client.{ Client, Config }
import com.github.dakatsuka.akka.http.oauth2.client.Error.UnauthorizedException
import com.github.dakatsuka.akka.http.oauth2.client.strategy._

import scala.concurrent.{ ExecutionContext, Future }

implicit val system: ActorSystem  = ActorSystem()
implicit val ec: ExecutionContext = system.dispatcher
implicit val mat: Materializer    = ActorMaterializer()

val config = Config(
  clientId     = "xxxxxxxxx",
  clientSecret = "xxxxxxxxx",
  site         = URI.create("https://api.example.com")
)

val client = Client(config)

// Some(https://api.example.com/oauth/authorize?redirect_uri=https://example.com/oauth2/callback&response_type=code&client_id=xxxxxxxxx)
val authorizeUrl: Option[Uri] =
  client.getAuthorizeUrl(GrantType.AuthorizationCode, Map("redirect_uri" -> "https://example.com/oauth2/callback"))

val accessToken: Future[Either[Throwable, AccessToken]] =
  client.getAccessToken(GrantType.AuthorizationCode, Map("code" -> "yyyyyy", "redirect_uri" -> "https://example.com"))

accessToken.foreach {
  case Right(t) =>
    t.accessToken  // String
    t.tokenType    // String
    t.expiresIn    // Int
    t.refreshToken // Option[String]
  case Left(ex: UnauthorizedException) =>
    ex.code        // Code
    ex.description // String
    ex.response    // HttpResponse
}

val newAccessToken: Future[Either[Throwable, AccessToken]] =
  client.getAccessToken(GrantType.RefreshToken, Map("refresh_token" -> "zzzzzzzz"))

Testing

Client can pass mock connection into constructor.

val mock = Flow[HttpRequest].map { _ =>
  HttpResponse(
    status = StatusCodes.OK,
    headers = Nil,
    entity = HttpEntity(
      `application/json`,
      s"""
         |{
         |  "access_token": "dummy",
         |  "token_type": "bearer",
         |  "expires_in": 86400,
         |  "refresh_token": "dummy"
         |}
      """.stripMargin
    )
  )
}

val client = Client(config, mock)

Authors

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.