Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
msarti committed Feb 28, 2014
1 parent 9a96f28 commit 1c349aa
Show file tree
Hide file tree
Showing 65 changed files with 3,479 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,11 @@ project/plugins/project/

# Scala-IDE specific
.scala_dependencies

#eclipse specific
.classpath
.project
.settings/



32 changes: 32 additions & 0 deletions modules/core/app/com/elogiclab/guardbee/core/AccessToken.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2014 Marco Sarti <marco.sarti at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.elogiclab.guardbee.core

/**
* @author Marco Sarti
*
*/
case class AccessToken(access_token:String, expires_in: Int)
47 changes: 47 additions & 0 deletions modules/core/app/com/elogiclab/guardbee/core/Authentication.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright (c) 2014 Marco Sarti <marco.sarti at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.elogiclab.guardbee.core

import org.joda.time.DateTime
import GuardbeeService._

/**
* @author Marco Sarti
*
*/
case class Authentication(username: String, provider: String, scope: Option[Seq[String]], lastAccess: DateTime = DateTime.now, remember_me: Boolean = false) {

def isExpired = lastAccess.plusMinutes(Configuration.SessionTimeout).isBefore(DateTime.now)

def isRoleGranted(role: String): Boolean = granted_roles.exists(v => v == role)

def touch = this.copy(lastAccess = DateTime.now)

def granted_roles: Seq[String] = Nil

def user[T <: User] = UserService[T].getByUsername(username)

}
66 changes: 66 additions & 0 deletions modules/core/app/com/elogiclab/guardbee/core/Authenticator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2014 Marco Sarti <marco.sarti at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.elogiclab.guardbee.core

import play.api.mvc.RequestHeader
import play.api.mvc.Request
import scala.concurrent._
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.data.Forms._
import play.api.data.Form

/**
* @author Marco Sarti
*
*/
trait Authenticator {

def ProviderId: String

type AuthenticationToken

def obtainDestinationUrl[A](request: Request[A]): Option[String] = {
Form(
single("dest" -> optional(text))
).bindFromRequest()(request).fold({ errors =>
None
}, { value =>
value
})
}

def obtainCredentials[A](request: Request[A]): Either[Errors, AuthenticationToken]

def authenticate(authToken: AuthenticationToken): Either[Errors, Authentication]

def performAuthentication[A](request: Request[A]): Either[Errors, Authentication] = {
for (
auth_token <- obtainCredentials(request).right;
authentication <- authenticate(auth_token).right
) yield authentication
}

}
57 changes: 57 additions & 0 deletions modules/core/app/com/elogiclab/guardbee/core/Configuration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright (c) 2014 Marco Sarti <marco.sarti at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.elogiclab.guardbee.core

import play.api.Application

/**
* @author Marco Sarti
*
*/
class Configuration(application: Application) {
implicit val app = application

val applicationKey = "guardbee"

lazy val SessionTimeout = application.configuration.getInt(applicationKey + ".session.timeout").getOrElse(60)

lazy val CookieName = application.configuration.getString(applicationKey + ".cookie.name").getOrElse("GUARDBEE_SESSION")
lazy val CookiePath = application.configuration.getString(applicationKey + ".cookie.path").getOrElse("/")
lazy val CookieSecure = application.configuration.getBoolean(applicationKey + ".cookie.secure").getOrElse(false)
lazy val CookieExpiration = application.configuration.getInt(applicationKey + ".cookie.expiration").getOrElse(3600)

lazy val ApplicationName = application.configuration.getString("application.name").getOrElse("Guardbee")

lazy val BcryptLogRounds = application.configuration.getInt(applicationKey + ".bcrypt.log_round").getOrElse(10)

lazy val DefaultProfileRoles: Seq[String] = {
import scala.collection.JavaConverters._
application.configuration.getStringList(applicationKey + ".google.defaultProfileRoles").map { l =>
l.asScala.toSeq
}.getOrElse(Seq("ROLE_USER"))
}

}
76 changes: 76 additions & 0 deletions modules/core/app/com/elogiclab/guardbee/core/Errors.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* Copyright (c) 2014 Marco Sarti <marco.sarti at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.elogiclab.guardbee.core

import play.api.mvc.Request
import play.api.i18n.Messages
import play.api.data.FormError


case class Msg(message: String, args: Any*)


/**
* @author Marco Sarti
*
*/
case class Errors(messages: Seq[Msg]) {

def add(message: Msg) = {
Errors(message +: messages)
}

def +(message: Msg) = add(message)

def printHTML[A](implicit request: Request[A]) = {
"<ul><li>"+messages.map( { msg => Messages(msg.message, msg.args:_*)} ).mkString("</li><li>")+"</li></ul>"
}

def print = {
messages.map( { msg => Messages(msg.message, msg.args:_*)} ).mkString(" - ")
}

}

object Errors {

val AuthenticationError = Errors(Seq(Msg("guardbee.error.authentication_error")))

def FormErrors(formErrors: Seq[FormError]) = {
Errors(formErrors.map { e => Msg(e.message, e.args:_*)})
}

def apply(message: String, args: Any*):Errors = Errors(Seq(Msg(message, args:_*)))


}

object ErrorCodes extends Enumeration {
type ErrorCode = Value

val ERROR_LDAP_UNAVAILABLE = Value

}

0 comments on commit 1c349aa

Please sign in to comment.