Created
November 26, 2019 19:19
-
-
Save CheatEx/2a76c06b3bf5480e62708161a6957b05 to your computer and use it in GitHub Desktop.
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
class LoginDemo { | |
type LoginResult = Either[UserInfo, String] | |
def Success[T, E] = Left[T, E] _ | |
def Failure[T, E] = Right[T, E] _ | |
//@rest.Method(httpMethod = Array(POST)) | |
def login(request: rest.Request, | |
//@rest.Param(name = "login") | |
loginParam: String, | |
//@rest.Param(name = "password") | |
passwordParam: String): Unit = | |
login(Option(loginParam), Option(passwordParam)) match { | |
case Left(user) => | |
request.session("user") = user | |
case Right(message) => | |
respond(UNAUTHORIZED, message) | |
} | |
def login(login: Option[String], password: Option[String]): LoginResult = | |
for (login <- login.toLeft( "You should provide login" ).left; | |
password <- password.toLeft( "You should provide password" ).left; | |
user <- findUser(login).left; | |
checkedUser <- checkUser(user).left; | |
loggedUser <- doLogin(checkedUser, login, password).left | |
) yield loggedUser | |
def findUser(login: String): LoginResult = | |
AccountsStorage.find(login).toLeft( "User not found" ) | |
def checkUser(user: UserInfo): LoginResult = | |
if (user.inactive) Failure("Account is inactive") | |
else Success(user) | |
def doLogin(user: UserInfo, login: String, password: String): LoginResult = | |
if (user.authScheme == "PETRIVKA") | |
handlePetrivkaAuthSchemeLogin(user, password) | |
else | |
handleUsualAuthSchemeLogin(user, login, password) | |
def handlePetrivkaAuthSchemeLogin(user: UserInfo, password: String): LoginResult = | |
if( user.passwordMatches(password) ) Success(user) | |
else Failure("Authentication failed") | |
def handleUsualAuthSchemeLogin(user: UserInfo, login: String, password: String) = | |
AccessStorage.access.auth_configs.find(_.key == user.authScheme) match { | |
case Some(scheme) => | |
//log.debug("authenticating with " + scheme.command) | |
val exec = Runtime.getRuntime.exec( | |
scheme.command replace("{login}", login) replace("{password}", password)) | |
if( exec.waitFor == 0 ) | |
Success(user) | |
else | |
Failure("Authentication within " + scheme + " failed") | |
case None => Failure("Unknown authentication scheme: " + user.authScheme) | |
} | |
def respond(code: Int, message:String = "") = {} | |
val UNAUTHORIZED = 401 | |
} | |
class UserInfo { | |
val inactive = false | |
val authScheme = "" | |
def passwordMatches(pwd: String) = true | |
} | |
object AccountsStorage { | |
def find(login: String): Option[UserInfo] = None | |
} | |
object AccessStorage { | |
object access { | |
object auth_configs { | |
def find(pred: {val key: String} => Boolean): Option[{val command: String}] = None | |
} | |
} | |
} | |
package rest { | |
class Request { | |
val session = scala.collection.mutable.Map[String, Any]() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment