joscha / play-authenticate

An authentication plugin for Play Framework 2.x (Java)
http://joscha.github.com/play-authenticate/
Other
807 stars 366 forks source link

NPE in OAuth2AuthProvider.java:191 #147

Closed reynirh closed 10 years ago

reynirh commented 10 years ago

I've been updating my play project to 2.2.1 and just added a dependency exclusion for play-cache as described in https://github.com/joscha/play-authenticate/issues/134#issuecomment-30080484

Now the project builds, and seems to work up to the point of authentication where it fails with the following NPE:

! @6h1n5622o - Internal server error, for (GET) [/authenticate/google?state=5...(somestatehere)&code=... somecodehere ->

play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
    at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
    at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
    at scala.Option.map(Option.scala:145) [scala-library.jar:na]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: java.lang.NullPointerException: null
    at com.feth.play.module.pa.providers.oauth2.OAuth2AuthProvider.authenticate(OAuth2AuthProvider.java:191) ~[play-authenticate_2.10-0.5.2-SNAPSHOT.jar:0.5.2-SNAPSHOT]
    at com.feth.play.module.pa.PlayAuthenticate.handleAuthentication(PlayAuthenticate.java:438) ~[play-authenticate_2.10-0.5.2-SNAPSHOT.jar:0.5.2-SNAPSHOT]
    at com.feth.play.module.pa.controllers.Authenticate.authenticate(Authenticate.java:25) ~[play-authenticate_2.10-0.5.2-SNAPSHOT.jar:0.5.2-SNAPSHOT]
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$15.apply(routes_routing.scala:289) ~[na:na]
    at Routes$$anonfun$routes$1$$anonfun$applyOrElse$15$$anonfun$apply$15.apply(routes_routing.scala:289) ~[na:na]
    at play.core.Router$HandlerInvoker$$anon$7$$anon$2.invocation(Router.scala:183) ~[play_2.10.jar:2.2.1] ```

I looked at the play-authenticate source, and it seems the cache might be returning null.
smola commented 10 years ago

Play-Authenticate requires play-cache. If you have no cache plugin loaded, the cache will return null and you'll get this issue.

reynirh commented 10 years ago

@smola if you check out the ticket https://github.com/joscha/play-authenticate/issues/134#issuecomment-30080484 , I do exactly what is recommended there, to fix a build problem. The build problem is fixed, so there for I must have a version of play-cache. Also it seems accessing play-cache is not throwing the NPE, but accessing the object returned from the cache.

smola commented 10 years ago

@reynirh Yes. It's ok to add that dependency exclusion to prevent other packages pulling the wrong version of play-cache. But you still need play-cache (just the correct version). So check that you have cache in your build file. For example, in build.sbt:

libraryDependencies ++= Seq(cache, ...)

or in Build.scala:

val appDependencies = Seq(cache ....)

If the problem persists after ensuring cache plugin is loaded, there might be another problem related to cookies. But let's see first if it's just the cache plugin problem.

razorman8669 commented 10 years ago

I am getting the same error and I have, I believe, all the cache dependencies setup correctly like so:

    val appDependencies = Seq(
       javaCore, javaJdbc, javaEbean, cache,
      "be.objectify"  %%  "deadbolt-java"     % "2.2-RC2" exclude("com.typesafe.play", "play-cache_2.10"),
      "be.objectify"          %%  "deadbolt-scala"     % "2.2-RC2" exclude("com.typesafe.play", "play-cache_2.10"),
      "com.feth"              %%  "play-authenticate"  % "0.5.2-SNAPSHOT",
      "postgresql"    %   "postgresql"          % "9.1-901.jdbc4",
      "commons-io"    %   "commons-io"          % "2.4",
      "commons-collections"  %   "commons-collections" %  "3.2.1",
      "joda-time"              %   "joda-time"           %  "2.3",
      "org.owasp.antisamy"  %  "antisamy"  %   "1.4.4"
    )

    val main = play.Project(appName, appVersion, appDependencies).settings(
      resolvers += Resolver.url("Objectify Play Repository (release)", url("http://schaloner.github.com/releases/"))(Resolver.ivyStylePatterns),
      resolvers += Resolver.url("Objectify Play Repository (snapshot)", url("http://schaloner.github.com/snapshots/"))(Resolver.ivyStylePatterns),

      resolvers += Resolver.url("play-easymail (release)", url("http://joscha.github.com/play-easymail/repo/releases/"))(Resolver.ivyStylePatterns),
      resolvers += Resolver.url("play-easymail (snapshot)", url("http://joscha.github.com/play-easymail/repo/snapshots/"))(Resolver.ivyStylePatterns),

      resolvers += Resolver.url("play-authenticate (release)", url("http://joscha.github.com/play-authenticate/repo/releases/"))(Resolver.ivyStylePatterns),
      resolvers += Resolver.url("play-authenticate (snapshot)", url("http://joscha.github.com/play-authenticate/repo/snapshots/"))(Resolver.ivyStylePatterns)
    )
smola commented 10 years ago

I started to get this problem again in a setup where cache should be properly set up. I'm looking into the issue again.

joscha commented 10 years ago

Can you check if the problem persists with deadbolt-2.2.1-RC1 and not excluding the cache dependency? See: https://github.com/schaloner/deadbolt-2/issues/41

razorman8669 commented 10 years ago

made the change, but now I am getting a different NPE after trying to authenticate:

play.api.Application$$anon$1: Execution exception[[NullPointerException: null]]
    at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
    at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
    at scala.Option.map(Option.scala:145) [scala-library.jar:na]
    at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: java.lang.NullPointerException: null
    at be.objectify.deadbolt.java.actions.AbstractRestrictiveAction.execute(AbstractRestrictiveAction.java:47) ~[deadbolt-java_2.10-2.2.1-RC1.jar:2.2.1-RC1]
    at be.objectify.deadbolt.java.actions.AbstractDeadboltAction.call(AbstractDeadboltAction.java:119) ~[deadbolt-java_2.10-2.2.1-RC1.jar:2.2.1-RC1]
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:91) ~[play_2.10.jar:2.2.1]
    at play.core.j.JavaAction$$anon$3.apply(JavaAction.scala:90) ~[play_2.10.jar:2.2.1]
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) ~[play_2.10.jar:2.2.1]
    at play.core.j.FPromiseHelper$$anonfun$flatMap$1.apply(FPromiseHelper.scala:82) ~[play_2.10.jar:2.2.1]
reynirh commented 10 years ago

just changing the cache library dependency in build.sbt fixed the problem for me. I'm not getting the above NPE. @razorman8669 perhaps you have to run play clean-all

razorman8669 commented 10 years ago

@reynirh, Running clean-all doesnt solve the NPE. not sure what is causing it yet...

joscha commented 10 years ago

The NPE is because null handling in deadbolt2 changed slightly. See here https://github.com/schaloner/deadbolt-2/issues/41 for an easy fix.

razorman8669 commented 10 years ago

the change fixed the issue. Thanks @joscha!