akka-http-oauth2-client


License

License

Categories

Categories

H2 Data Databases OAuth2 Security CLI User Interface Akka Container Microservices Reactive libraries
GroupId

GroupId

com.github.dakatsuka
ArtifactId

ArtifactId

akka-http-oauth2-client_2.12
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

akka-http-oauth2-client
akka-http-oauth2-client
Project URL

Project URL

https://github.com/dakatsuka/akka-http-oauth2-client
Project Organization

Project Organization

com.github.dakatsuka
Source Code Management

Source Code Management

https://github.com/dakatsuka/akka-http-oauth2-client

Download akka-http-oauth2-client_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.dakatsuka/akka-http-oauth2-client_2.12/ -->
<dependency>
    <groupId>com.github.dakatsuka</groupId>
    <artifactId>akka-http-oauth2-client_2.12</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.dakatsuka/akka-http-oauth2-client_2.12/
implementation 'com.github.dakatsuka:akka-http-oauth2-client_2.12:0.2.0'
// https://jarcasting.com/artifacts/com.github.dakatsuka/akka-http-oauth2-client_2.12/
implementation ("com.github.dakatsuka:akka-http-oauth2-client_2.12:0.2.0")
'com.github.dakatsuka:akka-http-oauth2-client_2.12:jar:0.2.0'
<dependency org="com.github.dakatsuka" name="akka-http-oauth2-client_2.12" rev="0.2.0">
  <artifact name="akka-http-oauth2-client_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.dakatsuka', module='akka-http-oauth2-client_2.12', version='0.2.0')
)
libraryDependencies += "com.github.dakatsuka" % "akka-http-oauth2-client_2.12" % "0.2.0"
[com.github.dakatsuka/akka-http-oauth2-client_2.12 "0.2.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.3
com.typesafe.akka : akka-http_2.12 jar 10.0.10
io.circe : circe-generic_2.12 jar 0.8.0
io.circe : circe-parser_2.12 jar 0.8.0

test (3)

Group / Artifact Type Version
com.typesafe.akka : akka-http-testkit_2.12 jar 10.0.10
org.scalatest : scalatest_2.12 jar 3.0.3
org.scalamock : scalamock-scalatest-support_2.12 jar 3.4.2

Project Modules

There are no modules declared in this project.

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

Versions

Version
0.2.0
0.1.0