-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#263 Add separate authSrv for authentication by key. Add removeKey AP…
…I. Add hasKey to user json output Enable authentication by key in module initialization (can't be disabled)
- Loading branch information
Showing
7 changed files
with
127 additions
and
43 deletions.
There are no files selected for viewing
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
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,59 @@ | ||
package services | ||
|
||
import java.util.Base64 | ||
import javax.inject.{ Inject, Singleton } | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.util.Random | ||
|
||
import play.api.libs.json.JsArray | ||
import play.api.mvc.RequestHeader | ||
|
||
import akka.stream.Materializer | ||
import akka.stream.scaladsl.Sink | ||
|
||
import org.elastic4play.controllers.Fields | ||
import org.elastic4play.services.{ AuthCapability, AuthContext, AuthSrv } | ||
import org.elastic4play.{ AuthenticationError, BadRequestError } | ||
|
||
@Singleton | ||
class KeyAuthSrv @Inject() ( | ||
userSrv: UserSrv, | ||
implicit val ec: ExecutionContext, | ||
implicit val mat: Materializer) extends AuthSrv { | ||
override val name = "key" | ||
|
||
protected final def generateKey(): String = { | ||
val bytes = Array.ofDim[Byte](24) | ||
Random.nextBytes(bytes) | ||
Base64.getEncoder.encodeToString(bytes) | ||
} | ||
|
||
override val capabilities = Set(AuthCapability.authByKey) | ||
|
||
override def authenticate(key: String)(implicit request: RequestHeader): Future[AuthContext] = { | ||
import org.elastic4play.services.QueryDSL._ | ||
// key attribute is sensitive so it is not possible to search on that field | ||
userSrv.find("status" ~= "Ok", Some("all"), Nil) | ||
._1 | ||
.filter(_.key().contains(key)) | ||
.runWith(Sink.headOption) | ||
.flatMap { | ||
case Some(user) ⇒ userSrv.getFromUser(request, user) | ||
case None ⇒ Future.failed(AuthenticationError("Authentication failure")) | ||
} | ||
} | ||
|
||
override def renewKey(username: String)(implicit authContext: AuthContext): Future[String] = { | ||
val newKey = generateKey() | ||
userSrv.update(username, Fields.empty.set("key", newKey)).map(_ ⇒ newKey) | ||
} | ||
|
||
override def getKey(username: String)(implicit authContext: AuthContext): Future[String] = { | ||
userSrv.get(username).map(_.key().getOrElse(throw BadRequestError(s"User $username hasn't key"))) | ||
} | ||
|
||
override def removeKey(username: String)(implicit authContext: AuthContext): Future[Unit] = { | ||
userSrv.update(username, Fields.empty.set("key", JsArray())).map(_ ⇒ ()) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package services | ||
|
||
import javax.inject.{ Inject, Singleton } | ||
|
||
import scala.collection.immutable | ||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.util.{ Failure, Success } | ||
|
||
import play.api.mvc.RequestHeader | ||
import play.api.{ Configuration, Logger } | ||
|
||
import org.elastic4play.AuthenticationError | ||
import org.elastic4play.services.{ AuthContext, AuthSrv } | ||
import org.elastic4play.services.auth.MultiAuthSrv | ||
|
||
object TheHiveAuthSrv { | ||
private[TheHiveAuthSrv] lazy val logger = Logger(getClass) | ||
|
||
def getAuthSrv(authTypes: Seq[String], authModules: immutable.Set[AuthSrv]): Seq[AuthSrv] = { | ||
("key" +: authTypes.filterNot(_ == "key")) | ||
.flatMap { authType ⇒ | ||
authModules.find(_.name == authType) | ||
.orElse { | ||
logger.error(s"Authentication module $authType not found") | ||
None | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Singleton | ||
class TheHiveAuthSrv @Inject() ( | ||
configuration: Configuration, | ||
authModules: immutable.Set[AuthSrv], | ||
userSrv: UserSrv, | ||
override implicit val ec: ExecutionContext) extends MultiAuthSrv( | ||
TheHiveAuthSrv.getAuthSrv( | ||
configuration.getOptional[Seq[String]]("auth.type").getOrElse(Seq("local")), | ||
authModules), | ||
ec) { | ||
|
||
// Uncomment the following lines if you want to prevent user with key to use password to authenticate | ||
// override def authenticate(username: String, password: String)(implicit request: RequestHeader): Future[AuthContext] = | ||
// userSrv.get(username) | ||
// .transformWith { | ||
// case Success(user) if user.key().isDefined ⇒ Future.failed(AuthenticationError("Authentication by password is not permitted for user with key")) | ||
// case _: Success[_] ⇒ super.authenticate(username, password) | ||
// case _: Failure[_] ⇒ Future.failed(AuthenticationError("Authentication failure")) | ||
// } | ||
} |
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