本文整理汇总了Scala中models.services.AuthTokenService类的典型用法代码示例。如果您正苦于以下问题:Scala AuthTokenService类的具体用法?Scala AuthTokenService怎么用?Scala AuthTokenService使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AuthTokenService类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ResetPasswordController
//设置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.{ PasswordHasherRegistry, PasswordInfo }
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.{ AuthTokenService, UserService }
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Controller
import utils.auth.DefaultEnv
import scala.concurrent.Future
class ResetPasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
authInfoRepository: AuthInfoRepository,
passwordHasherRegistry: PasswordHasherRegistry,
authTokenService: AuthTokenService
)
extends Controller with I18nSupport {
def view(token: UUID) = silhouette.UnsecuredAction.async { implicit request =>
authTokenService.validate(token).map {
case Some(authToken) => Ok(views.html.resetPassword(new myform.MyResetPasswordForm(), token))
case None => Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link"))
}
}
def submit(token: UUID) = silhouette.UnsecuredAction.async { implicit request =>
authTokenService.validate(token).flatMap {
case Some(authToken) =>
new myform.MyResetPasswordForm().bindFromRequest match {
case form: myform.MyResetPasswordForm => Future.successful(BadRequest(views.html.resetPassword(form, token)))
case data: myform.MyResetPasswordFormData => userService.retrieve(authToken.userID).flatMap {
case Some(user) if user.loginInfo.providerID == CredentialsProvider.ID =>
val passwordInfo = passwordHasherRegistry.current.hash(data.password)
authInfoRepository.update[PasswordInfo](user.loginInfo, passwordInfo).map { _ =>
Redirect(routes.SignInController.view()).flashing("success" -> Messages("password.reset"))
}
case _ => Future.successful(Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link")))
}
}
case None => Future.successful(Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link")))
}
}
}
示例2: ForgotPasswordController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.{ AuthTokenService, UserService }
import models.daos._
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.mailer.{ Email, MailerClient }
import play.api.mvc.Controller
import utils.auth.DefaultEnv
import scala.concurrent.Future
class ForgotPasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
authTokenService: AuthTokenService,
mailerClient: MailerClient,
envDAO: EnvDAO
)
extends Controller with I18nSupport {
def view = silhouette.UnsecuredAction.async { implicit request =>
Future.successful(Ok(views.html.forgotPassword(new myform.MyForgotPasswordForm())))
}
def submit = silhouette.UnsecuredAction.async { implicit request =>
new myform.MyForgotPasswordForm().bindFromRequest match {
case form: myform.MyForgotPasswordForm => Future.successful(BadRequest(views.html.forgotPassword(form)))
case data: myform.MyForgotPasswordFormData => {
val email = data.email
val loginInfo = LoginInfo(CredentialsProvider.ID, email)
val result = Redirect(routes.SignInController.view()).flashing("info" -> Messages("reset.email.sent"))
userService.retrieve(loginInfo).flatMap {
case Some(user) if user.email.isDefined =>
authTokenService.create(user.userID).map { authToken =>
val url = routes.ResetPasswordController.view(authToken.id).absoluteURL()
println("url " + url)
if (envDAO.getDosendmail) mailerClient.send(Email(
subject = Messages("email.reset.password.subject"),
from = Messages("email.from"),
to = Seq(email),
bodyText = Some(views.txt.emails.resetPassword(user, url).body),
bodyHtml = Some(views.html.emails.resetPassword(user, url).body)
))
result
}
case None => Future.successful(result)
}
}
}
}
}
示例3: Startup
//设置package包名称以及导入依赖的类
package jobs
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.PasswordHasherRegistry
import com.mohiva.play.silhouette.impl.providers._
import models.{ User, UserMajor }
import models.MajorType._
import models.services.{ AuthTokenService, UserService, MajorService, UserMajorService }
import utils.auth.DefaultEnv
import shared.Util.reverseEffect
import play.api.libs.concurrent.Execution.Implicits._
@Singleton
class Startup @Inject() (
silhouette: Silhouette[DefaultEnv],
userService: UserService,
majorService: MajorService,
userMajorService: UserMajorService,
authInfoRepository: AuthInfoRepository,
authTokenService: AuthTokenService,
passwordHasherRegistry: PasswordHasherRegistry) {
//Create Sample user
//Major: ??2013, ??2013
//
val loginInfo = LoginInfo(CredentialsProvider.ID, "[email protected]")
val authInfo = passwordHasherRegistry.current.hash("asdf")
for {
fmOption <- majorService.find("??????", FirstMajor, 2013)
smOption <- majorService.find("????????", SecondMajor, 2013)
userData = User(
userID = UUID.fromString("42944783-ee3f-4a92-85b9-777c94d9dc5e"),
loginInfo = loginInfo,
email = Some("[email protected]"),
classYear = 2013,
semester = 5,
activated = true
)
userMajors = Seq(fmOption, smOption).filterNot(_.isEmpty).map(_.get).map(m => UserMajor(userData.userID, m.id))
savedUserMajors <- reverseEffect(userMajors.map(userMajor => userMajorService.save(userMajor)))
user <- userService.save(userData)
authInfo <- authInfoRepository.add(loginInfo, authInfo)
authToken <- authTokenService.create(user.userID)
} yield ()
}