-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from toolsplus/bugfix/fix-asymmetric-installat…
…ion-lifecycle Fix asymmetrically signed lifecycle methods
- Loading branch information
Showing
18 changed files
with
726 additions
and
557 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
modules/core/app/io/toolsplus/atlassian/connect/play/actions/JwtAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.toolsplus.atlassian.connect.play.actions | ||
|
||
import io.toolsplus.atlassian.connect.play.auth.jwt.JwtCredentials | ||
import play.api.mvc.Results.Unauthorized | ||
import play.api.mvc.{ActionRefiner, Request, Result, WrappedRequest} | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
case class JwtRequest[A](credentials: JwtCredentials, request: Request[A]) | ||
extends WrappedRequest[A](request) | ||
|
||
/** | ||
* Play action refiner that extracts the JWT credentials from a request | ||
* | ||
* Note that this refiner will intercept the request and return an unauthorized | ||
* result if no JWT credentials were found. | ||
*/ | ||
class JwtActionRefiner @Inject()( | ||
implicit val executionContext: ExecutionContext) | ||
extends ActionRefiner[Request, JwtRequest] { | ||
|
||
override def refine[A]( | ||
request: Request[A]): Future[Either[Result, JwtRequest[A]]] = | ||
JwtExtractor.extractJwt(request) match { | ||
case Some(credentials) => | ||
Future.successful(Right(JwtRequest(credentials, request))) | ||
case None => | ||
Future.successful(Left(Unauthorized("No authentication token found"))) | ||
} | ||
} |
27 changes: 0 additions & 27 deletions
27
modules/core/app/io/toolsplus/atlassian/connect/play/actions/LifecycleActions.scala
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
...lassian/connect/play/actions/asymmetric/AsymmetricallySignedAtlassianHostUserAction.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.toolsplus.atlassian.connect.play.actions.asymmetric | ||
|
||
import cats.implicits._ | ||
import io.toolsplus.atlassian.connect.play.actions.{ | ||
JwtActionRefiner, | ||
JwtRequest | ||
} | ||
import io.toolsplus.atlassian.connect.play.api.models.AtlassianHostUser | ||
import io.toolsplus.atlassian.connect.play.auth.jwt._ | ||
import io.toolsplus.atlassian.connect.play.auth.jwt.asymmetric.AsymmetricJwtAuthenticationProvider | ||
import play.api.mvc.Results.Unauthorized | ||
import play.api.mvc._ | ||
|
||
import javax.inject.Inject | ||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
case class MaybeAtlassianHostUserRequest[A](hostUser: Option[AtlassianHostUser], | ||
request: JwtRequest[A]) | ||
extends WrappedRequest[A](request) | ||
|
||
case class AsymmetricallySignedAtlassianHostUserActionRefiner( | ||
jwtAuthenticationProvider: AsymmetricJwtAuthenticationProvider, | ||
qshProvider: QshProvider)(implicit val executionContext: ExecutionContext) | ||
extends ActionRefiner[JwtRequest, MaybeAtlassianHostUserRequest] { | ||
override def refine[A](request: JwtRequest[A]) | ||
: Future[Either[Result, MaybeAtlassianHostUserRequest[A]]] = { | ||
val expectedQsh = qshProvider match { | ||
case ContextQshProvider => ContextQshProvider.qsh | ||
case CanonicalHttpRequestQshProvider => | ||
CanonicalHttpRequestQshProvider.qsh( | ||
request.credentials.canonicalHttpRequest) | ||
} | ||
jwtAuthenticationProvider | ||
.authenticate(request.credentials, expectedQsh) | ||
.map(MaybeAtlassianHostUserRequest(_, request)) | ||
.leftMap(e => Unauthorized(s"JWT validation failed: ${e.getMessage}")) | ||
.value | ||
} | ||
} | ||
|
||
class AsymmetricallySignedAtlassianHostUserAction @Inject()( | ||
bodyParser: BodyParsers.Default, | ||
jwtActionRefiner: JwtActionRefiner, | ||
asymmetricJwtAuthenticationProvider: AsymmetricJwtAuthenticationProvider)( | ||
implicit executionCtx: ExecutionContext) { | ||
|
||
/** | ||
* Creates an action builder that validates asymmetrically signed JWT requests. Callers must specify | ||
* how the query string hash claim should be verified. | ||
* | ||
* @param qshProvider Query string hash provider that specifies what kind of QSH the qsh claim contains | ||
* @return Play action for asymmetrically signed JWT requests | ||
*/ | ||
def authenticateWith(qshProvider: QshProvider) | ||
: ActionBuilder[MaybeAtlassianHostUserRequest, AnyContent] = | ||
new ActionBuilder[MaybeAtlassianHostUserRequest, AnyContent] { | ||
override val parser: BodyParsers.Default = bodyParser | ||
override val executionContext: ExecutionContext = executionCtx | ||
override def invokeBlock[A]( | ||
request: Request[A], | ||
block: MaybeAtlassianHostUserRequest[A] => Future[Result]) | ||
: Future[Result] = { | ||
(jwtActionRefiner andThen AsymmetricallySignedAtlassianHostUserActionRefiner( | ||
asymmetricJwtAuthenticationProvider, | ||
qshProvider)) | ||
.invokeBlock(request, block) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.