本文整理汇总了Scala中com.mohiva.play.silhouette.api.util.Credentials类的典型用法代码示例。如果您正苦于以下问题:Scala Credentials类的具体用法?Scala Credentials怎么用?Scala Credentials使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Credentials类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ChangePasswordController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.{ Credentials, PasswordHasherRegistry, PasswordInfo }
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.UserService
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Controller
import utils.auth.{ DefaultEnv, WithProvider }
import scala.concurrent.Future
class ChangePasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
credentialsProvider: CredentialsProvider,
authInfoRepository: AuthInfoRepository,
passwordHasherRegistry: PasswordHasherRegistry
)
extends Controller with I18nSupport {
def view = silhouette.SecuredAction(WithProvider[DefaultEnv#A](CredentialsProvider.ID)) { implicit request =>
Ok(views.html.changePassword(new myform.MyChangePasswordForm(), request.identity))
}
def submit = silhouette.SecuredAction(WithProvider[DefaultEnv#A](CredentialsProvider.ID)).async { implicit request =>
new myform.MyChangePasswordForm().bindFromRequest match {
case form: myform.MyChangePasswordForm => Future.successful(BadRequest(views.html.changePassword(form, request.identity)))
case data: myform.MyChangePasswordFormData => {
val (currentPassword, newPassword) = (data.currentPassword, data.newPassword)
val credentials = Credentials(request.identity.email.getOrElse(""), currentPassword)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val passwordInfo = passwordHasherRegistry.current.hash(newPassword)
authInfoRepository.update[PasswordInfo](loginInfo, passwordInfo).map { _ =>
Redirect(routes.ChangePasswordController.view()).flashing("success" -> Messages("password.changed"))
}
}.recover {
case e: ProviderException =>
Redirect(routes.ChangePasswordController.view()).flashing("error" -> Messages("current.password.invalid"))
}
}
}
}
}
示例2: UserForms
//设置package包名称以及导入依赖的类
package models.user
import com.mohiva.play.silhouette.api.util.Credentials
import play.api.data._
import play.api.data.Forms._
object UserForms {
val signInForm = Form(
mapping(
"email" -> email,
"password" -> nonEmptyText
)(Credentials.apply)(Credentials.unapply)
)
val registrationForm = Form(
mapping(
"username" -> nonEmptyText,
"email" -> nonEmptyText,
"password" -> nonEmptyText
)(RegistrationData.apply)(RegistrationData.unapply)
)
}
示例3: authenticate
//设置package包名称以及导入依赖的类
package controllers.security.rest
import scala.concurrent.Future
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.concurrent.Execution.Implicits._
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ConfigurationException
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator
import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException
import com.mohiva.play.silhouette.api.util.Credentials
import modules.cake.HeaderEnvironmentModule
import security.models._
import models.users.User
def authenticate = Action.async(parse.json[Credentials]) { implicit request =>
(env.providers.get(CredentialsProvider.ID) match {
case Some(p: CredentialsProvider) => p.authenticate(request.body)
case _ => Future.failed(new ConfigurationException(s"Cannot find credentials provider"))
}).flatMap { loginInfo =>
userService.retrieve(loginInfo).flatMap {
case Some(user) => env.authenticatorService.create(user.loginInfo).flatMap { authenticator =>
env.eventBus.publish(LoginEvent(user, request, request2lang))
env.authenticatorService.init(authenticator).flatMap { token =>
env.authenticatorService.embed(token, Future.successful {
Ok(Json.toJson(Token(token = token, expiresOn = authenticator.expirationDate)))
})
}
}
case None =>
Future.failed(new IdentityNotFoundException("Couldn't find user"))
}
}.recoverWith(exceptionHandler)
}
}
object RestCredentialsAuthController extends RestCredentialsAuthController
示例4: Authentication
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.util.Credentials
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException
import com.mohiva.play.silhouette.impl.providers._
import forms.SignInForm
import models.User
import models.daos.UserDAO
import play.api.i18n.{Messages, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Action
import scala.concurrent.Future
class Authentication @Inject()(val messagesApi: MessagesApi,
val env: Environment[User, SessionAuthenticator],
userDAO: UserDAO,
credentialsProvider: CredentialsProvider)
extends Silhouette[User, SessionAuthenticator] {
def authenticate = Action.async { implicit request =>
SignInForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(views.html.signIn(form))),
data => {
val credentials = Credentials(data.email, data.password)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val result = Redirect(routes.Application.dashboard())
userDAO.retrieve(loginInfo).flatMap {
case Some(user) =>
env.authenticatorService.create(loginInfo).flatMap { authenticator =>
env.eventBus.publish(LoginEvent(user, request, request2Messages))
env.authenticatorService.init(authenticator).flatMap { v =>
env.authenticatorService.embed(v, result)
}
}
case None => Future.failed(new IdentityNotFoundException(Messages("user.not_found")))
}
}.recover {
case _: ProviderException =>
Redirect(routes.Application.signIn()).flashing("error" -> Messages("invalid.credentials"))
}
}
)
}
}