本文整理汇总了Scala中com.mohiva.play.silhouette.api.util.PasswordHasher类的典型用法代码示例。如果您正苦于以下问题:Scala PasswordHasher类的具体用法?Scala PasswordHasher怎么用?Scala PasswordHasher使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PasswordHasher类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: PasswordHasherSHA256
//设置package包名称以及导入依赖的类
package controllers
import java.nio.charset.StandardCharsets
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants
import org.apache.directory.api.ldap.model.password.PasswordUtil
class PasswordHasherSHA256 extends PasswordHasher{
override def id: String = "SHA-256"
override def hash(plainPassword: String): PasswordInfo = {
PasswordInfo(hasher = id, password = plainPassword)
}
//TODO: Here might be an oversight. It seems to be to complicated.
override def matches(passwordInfo: PasswordInfo, suppliedPassword: String): Boolean = {
val suppliedStoragePW = PasswordUtil.createStoragePassword(
suppliedPassword,
LdapSecurityConstants.getAlgorithm(passwordInfo.hasher)
)
val suppliedPW = new String(PasswordUtil.splitCredentials(suppliedStoragePW).getPassword,StandardCharsets.UTF_8)
val storedPW = passwordInfo.password
suppliedPW.equals(storedPW)
}
}
示例2: PasswordHasherMD5
//设置package包名称以及导入依赖的类
package controllers
import java.nio.charset.StandardCharsets
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants
import org.apache.directory.api.ldap.model.password.PasswordUtil
class PasswordHasherMD5 extends PasswordHasher{
override def id: String = "MD5"
override def hash(plainPassword: String): PasswordInfo = {
PasswordInfo(hasher = id, password = plainPassword)
}
//TODO: Here might be an oversight. It seems to be to complicated.
override def matches(passwordInfo: PasswordInfo, suppliedPassword: String): Boolean = {
val suppliedStoragePW = PasswordUtil.createStoragePassword(
suppliedPassword,
LdapSecurityConstants.getAlgorithm(passwordInfo.hasher)
)
val suppliedPW = new String(PasswordUtil.splitCredentials(suppliedStoragePW).getPassword,StandardCharsets.UTF_8)
val storedPW = passwordInfo.password
suppliedPW.equals(storedPW)
}
}
示例3: RegistrationController
//设置package包名称以及导入依赖的类
package controllers
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordHasherRegistry}
import com.mohiva.play.silhouette.api.{LoginEvent, LoginInfo, SignUpEvent, Silhouette}
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import com.omis.UserReg
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.libs.json.Json
import play.api.mvc.Action
import services.{PasswordInfoService, UserService}
import utils.auth.DefaultEnv
import scala.concurrent.{ExecutionContext, Future}
class RegistrationController(userService: UserService, passwordInfoService: PasswordInfoService,
val messagesApi: MessagesApi,
passwordHasher: PasswordHasher,
silhouette: Silhouette[DefaultEnv])(implicit val ec: ExecutionContext) extends BaseController(silhouette) with I18nSupport {
def register = Action.async(parse.json) { implicit request =>
request.body.validate[UserReg].map { data =>
val loginInfo = LoginInfo(CredentialsProvider.ID, data.regCode)
userService.retrieve(loginInfo).flatMap {
case Some(user) =>
val passwordInfo = passwordHasher.hash(data.password)
for {
_ <- passwordInfoService.add(loginInfo, passwordInfo)
authenticator <- silhouette.env.authenticatorService.create(loginInfo)
token <- silhouette.env.authenticatorService.init(authenticator)
} yield {
silhouette.env.eventBus.publish(SignUpEvent(user, request))
silhouette.env.eventBus.publish(LoginEvent(user, request))
Ok(token)
}
case None =>
Future.successful(BadRequest(Json.obj("message" -> Messages("User not found."))))
}
}.recoverTotal {
case error =>
Future.successful(Unauthorized(Json.obj("message" -> Messages("Invalid data"))))
}
}
}
示例4: messagesApi
//设置package包名称以及导入依赖的类
package controllers
import com.mohiva.play.silhouette.api.actions._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services.AvatarService
import com.mohiva.play.silhouette.api.util.{ Clock, PasswordHasher }
import com.mohiva.play.silhouette.impl.providers.{ CredentialsProvider, SocialProviderRegistry }
import com.softwaremill.macwire._
import models.daos.AuthTokenDAOImpl
import models.services.{ AuthTokenServiceImpl, UserService }
import play.api.Configuration
import play.api.i18n.MessagesApi
import utils.auth.{ CustomSecuredErrorHandler, CustomUnsecuredErrorHandler }
trait ControllerModule {
lazy val authToken = new AuthTokenDAOImpl
lazy val authTokenService = new AuthTokenServiceImpl(authToken, clock)
lazy val securedErrorHandler: SecuredErrorHandler = wire[CustomSecuredErrorHandler]
lazy val unSecuredErrorHandler: UnsecuredErrorHandler = wire[CustomUnsecuredErrorHandler]
lazy val userAwareAction = new DefaultUserAwareAction(new DefaultUserAwareRequestHandler)
lazy val securedAction: SecuredAction = new DefaultSecuredAction(new DefaultSecuredRequestHandler(securedErrorHandler))
lazy val unsecuredAction: UnsecuredAction = new DefaultUnsecuredAction(new DefaultUnsecuredRequestHandler(unSecuredErrorHandler))
def messagesApi: MessagesApi
def socialProviderRegistry: SocialProviderRegistry
def userService: UserService
def authInfoRepository: AuthInfoRepository
def credentialsProvider: CredentialsProvider
def configuration: Configuration
def clock: Clock
def avatarService: AvatarService
def passwordHasher: PasswordHasher
}
示例5: messagesApi
//设置package包名称以及导入依赖的类
package com.github.jacobono.modules
import com.mohiva.play.silhouette.api.Environment
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services.AvatarService
import com.mohiva.play.silhouette.api.util.Clock
import com.mohiva.play.silhouette.api.util.PasswordHasher
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import com.mohiva.play.silhouette.impl.providers.{ CredentialsProvider, SocialProviderRegistry }
import com.softwaremill.macwire._
import play.api.Configuration
import play.api.i18n.MessagesApi
import com.github.jacobono.controllers._
import com.github.jacobono.models.User
import com.github.jacobono.models.services.UserService
import com.github.jacobono.utils.CSRFHelper
trait ControllerModule {
def messagesApi: MessagesApi
def silhouetteEnvironment: Environment[User, CookieAuthenticator]
def socialProviderRegistry: SocialProviderRegistry
def csrfHelper: CSRFHelper
def userService: UserService
def authInfoRepository: AuthInfoRepository
def credentialsProvider: CredentialsProvider
def configuration: Configuration
def clock: Clock
def avatarService: AvatarService
def passwordHasher: PasswordHasher
lazy val applicationController: Application = wire[Application]
lazy val credentialsAuthController: CredentialsAuthController = wire[CredentialsAuthController]
lazy val signUpController: SignUpController = wire[SignUpController]
lazy val socialAuthController: SocialAuthController = wire[SocialAuthController]
}
示例6: SignUp
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.PasswordHasher
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import com.mohiva.play.silhouette.impl.providers._
import forms.SignUpForm
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 SignUp @Inject()(val messagesApi: MessagesApi,
val env: Environment[User, SessionAuthenticator],
userDAO: UserDAO,
authInfoRepository: AuthInfoRepository,
passwordHasher: PasswordHasher)
extends Silhouette[User, SessionAuthenticator] {
def signUp = Action.async { implicit request =>
SignUpForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(views.html.signUp(form))),
data => {
val loginInfo = LoginInfo(CredentialsProvider.ID, data.email)
userDAO.retrieve(loginInfo).flatMap {
case Some(_) =>
Future.successful(Redirect(routes.Application.signUp()).flashing("error" -> Messages("user.exists")))
case None =>
val authInfo = passwordHasher.hash(data.password)
val user = User(
id = UUID.randomUUID(),
name = data.name,
email = data.email
)
for {
user <- userDAO.save(user)
_ <- authInfoRepository.add(loginInfo, authInfo)
authenticator <- env.authenticatorService.create(loginInfo)
value <- env.authenticatorService.init(authenticator)
result <- env.authenticatorService.embed(value, Redirect(routes.Application.dashboard()))
} yield {
env.eventBus.publish(SignUpEvent(user, request, request2Messages))
env.eventBus.publish(LoginEvent(user, request, request2Messages))
result
}
}
}
)
}
}
示例7: AuthenticationController
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import javax.inject.Inject
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.PasswordHasher
import com.mohiva.play.silhouette.api.{LoginEvent, LoginInfo, SignUpEvent, Silhouette}
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import model.{LoginUser, User}
import modules.JWTEnv
import play.api.i18n.MessagesApi
import play.api.libs.json.{JsError, Json}
import play.api.mvc.{Action, Controller}
import service.UserService
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class AuthenticationController @Inject()(val messagesApi: MessagesApi,
silhouette: Silhouette[JWTEnv],
userService: UserService,
authInfoRepository: AuthInfoRepository,
passwordHasher: PasswordHasher) extends Controller {
implicit val signUpFormat = Json.format[LoginUser]
def signUp = Action.async(parse.json) { implicit request =>
request.body.validate[LoginUser].map { credentials =>
val user = User(userID = UUID.randomUUID(), name = credentials.email, LoginInfo(CredentialsProvider.ID, credentials.email))
userService.retrieve(user.loginInfo).flatMap {
case Some(u) => Future.successful(Conflict(Json.obj("error" -> ("user " + credentials.email + " already registered"))))
case None =>
for {
user <- userService.save(user).map(_ => user)
authInfo <- authInfoRepository.add(user.loginInfo, passwordHasher.hash(credentials.password))
authenticator <- silhouette.env.authenticatorService.create(user.loginInfo)
token <- silhouette.env.authenticatorService.init(authenticator)
result <- silhouette.env.authenticatorService.embed(token, Ok(Json.toJson(token)))
} yield {
silhouette.env.eventBus.publish(SignUpEvent(user, request))
silhouette.env.eventBus.publish(LoginEvent(user, request))
result
}
}
}.recoverTotal {
case error => Future.successful(BadRequest(JsError.toJson(error)))
}
}
}
开发者ID:team3,项目名称:Play2-mongodb-silhouette-activator-template,代码行数:52,代码来源:AuthenticationController.scala
示例8: PasswordAuthInfoDAO
//设置package包名称以及导入依赖的类
package models.daos
import javax.inject.Inject
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import org.example.project.schema.{Account, Query}
import xyz.mattclifton.autoquery.components.UpdateValue
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
class PasswordAuthInfoDAO @Inject() (query: Query, passwordHasher: PasswordHasher) extends DelegableAuthInfoDAO[PasswordInfo] {
def find(loginInfo: LoginInfo): Future[Option[PasswordInfo]] = {
Future(getAccount(loginInfo).flatMap(toPasswordInfo(_)))
}
def update(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[PasswordInfo] = {
getAccount(loginInfo).map(account => {
query.updateAccounts(
filter = _.apply(ids = Seq(account.id)),
values = _.apply(passwordHash = UpdateValue(Option(authInfo.password)), passwordSalt = UpdateValue(authInfo.salt))).execute()
})
Future(authInfo)
}
def remove(loginInfo: LoginInfo): Future[Unit] = {
getAccount(loginInfo).map(account => {
query.updateAccounts(
filter = _.apply(ids = Seq(account.id)),
values = _.apply(passwordHash = UpdateValue(None), passwordSalt = UpdateValue(None))).execute()
})
Future({})
}
def save(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[PasswordInfo] = {
update(loginInfo, authInfo)
}
def add(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[PasswordInfo] = {
update(loginInfo, authInfo)
}
private def getAccount(loginInfo: LoginInfo): Option[Account] = {
query.getAccountProviders(
filter = _.apply(providerIdAndProviderKey = Some(_.apply(Seq(loginInfo.providerID), Seq(loginInfo.providerKey)))),
include = s => Seq(s.account)).execute().headOption.map(_.account)
}
private def toPasswordInfo(account: Account): Option[PasswordInfo] = {
account.passwordHash.map(hash => PasswordInfo(passwordHasher.id, hash, account.passwordSalt))
}
}