All Projects → nulab → play2-oauth2-provider

nulab / play2-oauth2-provider

Licence: MIT License
This library is enabled using scala-oauth2-provider in Play Framework

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to play2-oauth2-provider

play-java-seed.g8
Play Java Seed template: use "sbt new playframework/play-java-seed.g8"
Stars: ✭ 20 (-28.57%)
Mutual labels:  playframework
scalikejdbc-play-support
Play Framework support
Stars: ✭ 55 (+96.43%)
Mutual labels:  playframework
play-scala-chatroom-example
Play chatroom with Scala API
Stars: ✭ 43 (+53.57%)
Mutual labels:  playframework
play-scala-log4j2-example
An example Play project using Log4J 2 as the logging engine
Stars: ✭ 14 (-50%)
Mutual labels:  playframework
play-scala-seed.g8
Play Scala Seed Template: run "sbt new playframework/play-scala-seed.g8"
Stars: ✭ 66 (+135.71%)
Mutual labels:  playframework
playsonify
An opinionated micro-framework to help you build practical JSON APIs with Play Framework (or akka-http)
Stars: ✭ 42 (+50%)
Mutual labels:  playframework
sbt-play-npm
Integrate a Npm application with Play framework
Stars: ✭ 10 (-64.29%)
Mutual labels:  playframework
block-explorer
The official Stakenet block explorer
Stars: ✭ 29 (+3.57%)
Mutual labels:  playframework
play-java-rest-api-example
REST API using Play in Java
Stars: ✭ 44 (+57.14%)
Mutual labels:  playframework
DomoScala
Home automation, with Scala and Arduino. Nerdy stuff.
Stars: ✭ 21 (-25%)
Mutual labels:  playframework
social-graph-api
Authentication & Social Graph API built on top of Redis, Neo4J and Play!
Stars: ✭ 13 (-53.57%)
Mutual labels:  playframework
play-grpc
Play + Akka gRPC
Stars: ✭ 31 (+10.71%)
Mutual labels:  playframework
play-redis
Redis Module for Play Framework
Stars: ✭ 11 (-60.71%)
Mutual labels:  playframework
play-json-extra
Play Json Extra extends Play Json with several opinionated features
Stars: ✭ 22 (-21.43%)
Mutual labels:  playframework
play2scalaz
Scalaz typeclasses <~> Playframework2 typeclasses
Stars: ✭ 14 (-50%)
Mutual labels:  playframework
subscriptions-frontend
sites.google.com/a/guardian.co.uk/guardian-subscriptions/
Stars: ✭ 15 (-46.43%)
Mutual labels:  playframework
play-scala-streaming-example
Example Play application showing Comet and Server Sent Events in Scala
Stars: ✭ 42 (+50%)
Mutual labels:  playframework
play-scala-anorm-example
Example Play Database Application using Anorm
Stars: ✭ 41 (+46.43%)
Mutual labels:  playframework
play-java-ebean-example
Example Play application showing Java with Ebean
Stars: ✭ 54 (+92.86%)
Mutual labels:  playframework
play-rconf
Remote configuration for Play Framework
Stars: ✭ 17 (-39.29%)
Mutual labels:  playframework

play2-oauth2-provider CI

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].