play2-oauth2-provider

Support scala-oauth2-core library on Play Framework Scala

License

License

Categories

Categories

IDE Development Tools H2 Data Databases OAuth2 Security
GroupId

GroupId

com.nulab-inc
ArtifactId

ArtifactId

play2-oauth2-provider_2.11
Last Version

Last Version

1.4.2
Release Date

Release Date

Type

Type

jar
Description

Description

play2-oauth2-provider
Support scala-oauth2-core library on Play Framework Scala
Project URL

Project URL

https://github.com/nulab/play2-oauth2-provider
Project Organization

Project Organization

com.nulab-inc
Source Code Management

Source Code Management

https://github.com/nulab/play2-oauth2-provider

Download play2-oauth2-provider_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.nulab-inc/play2-oauth2-provider_2.11/ -->
<dependency>
    <groupId>com.nulab-inc</groupId>
    <artifactId>play2-oauth2-provider_2.11</artifactId>
    <version>1.4.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.nulab-inc/play2-oauth2-provider_2.11/
implementation 'com.nulab-inc:play2-oauth2-provider_2.11:1.4.2'
// https://jarcasting.com/artifacts/com.nulab-inc/play2-oauth2-provider_2.11/
implementation ("com.nulab-inc:play2-oauth2-provider_2.11:1.4.2")
'com.nulab-inc:play2-oauth2-provider_2.11:jar:1.4.2'
<dependency org="com.nulab-inc" name="play2-oauth2-provider_2.11" rev="1.4.2">
  <artifact name="play2-oauth2-provider_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.nulab-inc', module='play2-oauth2-provider_2.11', version='1.4.2')
)
libraryDependencies += "com.nulab-inc" % "play2-oauth2-provider_2.11" % "1.4.2"
[com.nulab-inc/play2-oauth2-provider_2.11 "1.4.2"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.12

provided (2)

Group / Artifact Type Version
com.nulab-inc : scala-oauth2-core_2.11 jar 1.4.0
com.typesafe.play : play_2.11 jar 2.7.3

test (3)

Group / Artifact Type Version
com.typesafe.play : play-test_2.11 jar 2.7.3
org.scalatest : scalatest_2.11 jar 3.0.8
ch.qos.logback : logback-classic jar 1.2.3

Project Modules

There are no modules declared in this project.

play2-oauth2-provider Build Status

This library is enabled using scala-oauth2-provider in Play Framework.

Setup

Add "play2-oauth2-provider" to library dependencies of your project.

libraryDependencies ++= Seq(
  "com.nulab-inc" %% "scala-oauth2-core" % "1.5.0",
  "com.nulab-inc" %% "play2-oauth2-provider" % "1.5.0"
)
Library version Play version
1.5.0 2.8.x
1.4.2 2.7.x
1.3.0 2.6.x
1.2.0 2.5.x
0.16.1 2.4.x
0.14.0 2.3.x
0.7.4 2.2.x

How to use

You should follow four steps below to work with Play Framework.

  • Customizing Grant Handlers
  • Define a controller to issue access token
  • Assign a route to the controller
  • Access to an authorized resource

You want to use which grant types are supported or to use a customized handler for a grant type, you should override the handlers map in a customized TokenEndpoint trait.

class MyTokenEndpoint extends TokenEndpoint {
  override val handlers = Map(
    OAuthGrantType.AUTHORIZATION_CODE -> new AuthorizationCode(),
    OAuthGrantType.REFRESH_TOKEN -> new RefreshToken(),
    OAuthGrantType.CLIENT_CREDENTIALS -> new ClientCredentials(),
    OAuthGrantType.PASSWORD -> new Password(),
    OAuthGrantType.IMPLICIT -> new Implicit()
  )
}

Here's an example of a customized TokenEndpoint that 1) only supports the password grant type, and 2) customizes the password grant type handler to not require client credentials:

class MyTokenEndpoint extends TokenEndpoint {
  val passwordNoCred = new Password() {
    override def clientCredentialRequired = false
  }

  override val handlers = Map(
    OAuthGrantType.PASSWORD -> passwordNoCred
  )
}

Define your own controller with mixining OAuth2Provider trait provided by this library to issue access token with customized TokenEndpoint.

class MyController @Inject() (components: ControllerComponents)
  extends AbstractController(components) with OAuth2Provider {
  override val tokenEndpoint = new MyTokenEndpoint()

  def accessToken = Action.async { implicit request =>
    issueAccessToken(new MyDataHandler())
  }
}

Then, assign a route to the controller that OAuth clients will access to.

POST    /oauth2/access_token                    controllers.OAuth2Controller.accessToken

Finally, you can access to an authorized resource like this:

class MyController @Inject() (components: ControllerComponents)
  extends AbstractController(components) with OAuth2Provider {

  val action = Action.async { request =>
    authorize(new MockDataHandler()) { authInfo =>
      val user = authInfo.user // User is defined on your system
      // access resource for the user
      ???
    }
  }
}

If you'd like to change the OAuth workflow, modify handleRequest methods of TokenEndPoint and ProtectedResource traits.

Using Action composition

You can write more easily authorize action by using Action composition.

Play Framework's documentation is here.

class MyController @Inject() (components: ControllerComponents)
  extends AbstractController(components) with OAuth2ProviderActionBuilders {

  def list = AuthorizedAction(new MyDataHandler()) { request =>
    val user = request.authInfo.user // User is defined on your system
    // access resource for the user
  }
}

Examples

Play Framework 2.5

Play Framework 2.3

Play Framework 2.2

com.nulab-inc

Nulab Inc

We are a small company with a big heart and all of our products are handcrafted with love.

Versions

Version
1.4.2
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0
0.18.0
0.17.2
0.17.1
0.17.0
0.16.1
0.16.0
0.15.1
0.15.0
0.14.0
0.13.3
0.13.2
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.0
0.9.1
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0