-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from querqy/smui_saml
Smui saml
- Loading branch information
Showing
15 changed files
with
246 additions
and
549 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package filters | ||
|
||
import modules.SecurityModule | ||
import org.pac4j.play.filters.SecurityFilter | ||
import play.api.Configuration | ||
import play.api.http.HttpFilters | ||
import play.api.mvc.EssentialFilter | ||
|
||
import javax.inject.Inject | ||
|
||
class Filters @Inject()(configuration: Configuration, securityFilter: SecurityFilter) extends HttpFilters { | ||
|
||
override def filters: Seq[EssentialFilter] = { | ||
// The securityFilter applies the pac4j security rules. It is only used if a non-empty authentication | ||
// client has been configured. | ||
if (configuration.getOptional[String](SecurityModule.ConfigKeyAuthClient).exists(_.nonEmpty)) { | ||
Seq(securityFilter) | ||
} else { | ||
Seq.empty | ||
} | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package modules | ||
|
||
import com.google.inject.{AbstractModule, Provides} | ||
import org.pac4j.core.client.Clients | ||
import org.pac4j.core.client.direct.AnonymousClient | ||
import org.pac4j.core.config.Config | ||
import org.pac4j.core.context.session.SessionStore | ||
import org.pac4j.core.profile.CommonProfile | ||
import org.pac4j.play.scala.{DefaultSecurityComponents, Pac4jScalaTemplateHelper, SecurityComponents} | ||
import org.pac4j.play.store.{PlayCookieSessionStore, ShiroAesDataEncrypter} | ||
import org.pac4j.play.{CallbackController, LogoutController} | ||
import org.pac4j.saml.client.SAML2Client | ||
import org.pac4j.saml.config.SAML2Configuration | ||
import play.api.{Configuration, Environment} | ||
|
||
import java.nio.charset.StandardCharsets | ||
|
||
class SecurityModule(environment: Environment, configuration: Configuration) extends AbstractModule { | ||
|
||
import SecurityModule._ | ||
|
||
private val baseUrl = configuration.get[String]("smui.auth.baseUrl") | ||
|
||
override def configure(): Unit = { | ||
val sKey = configuration.get[String]("play.http.secret.key").substring(0, 16) | ||
val dataEncrypter = new ShiroAesDataEncrypter(sKey.getBytes(StandardCharsets.UTF_8)) | ||
val playSessionStore = new PlayCookieSessionStore(dataEncrypter) | ||
bind(classOf[SessionStore]).toInstance(playSessionStore) | ||
bind(classOf[SecurityComponents]).to(classOf[DefaultSecurityComponents]) | ||
bind(classOf[Pac4jScalaTemplateHelper[CommonProfile]]) | ||
|
||
// callback | ||
val callbackController = new CallbackController() | ||
callbackController.setDefaultUrl("/") | ||
bind(classOf[CallbackController]).toInstance(callbackController) | ||
|
||
// logout | ||
val logoutController = new LogoutController() | ||
logoutController.setDefaultUrl("/") | ||
bind(classOf[LogoutController]).toInstance(logoutController) | ||
} | ||
|
||
@Provides | ||
def provideConfig(): Config = { | ||
val maybeConfiguredClientName = configuration.getOptional[String](ConfigKeyAuthClient).filter(_.nonEmpty) | ||
val authClientOpt = maybeConfiguredClientName.map { | ||
case "SAML2Client" => createSaml2Client(s"$ConfigKeyPrefixClientConfig.SAML2Client") | ||
case other => throw new RuntimeException(s"Unsupported auth client config value: $other") | ||
} | ||
val allClients = authClientOpt.toSeq :+ new AnonymousClient() | ||
// callback URL path as configured in `routes` | ||
val clients = new Clients(s"$baseUrl/callback", allClients:_*) | ||
new Config(clients) | ||
} | ||
private def createSaml2Client(keyPrefix: String): SAML2Client = { | ||
val cfg = new SAML2Configuration( | ||
configuration.get[String](s"$keyPrefix.keystore"), | ||
configuration.get[String](s"$keyPrefix.keystorePassword"), | ||
configuration.get[String](s"$keyPrefix.privateKeyPassword"), | ||
configuration.get[String](s"$keyPrefix.identityProviderMetadataPath") | ||
) | ||
cfg.setServiceProviderEntityId(configuration.get[String](s"$keyPrefix.serviceProviderEntityId")) | ||
cfg.setServiceProviderMetadataPath(configuration.get[String](s"$keyPrefix.serviceProviderMetadataPath")) | ||
cfg.setMaximumAuthenticationLifetime(configuration.get[Long](s"$keyPrefix.maximumAuthenticationLifetime")) | ||
new SAML2Client(cfg) | ||
} | ||
|
||
} | ||
|
||
object SecurityModule { | ||
val ConfigKeyAuthClient = "smui.auth.client" | ||
val ConfigKeyPrefixClientConfig = "smui.auth.clients" | ||
} |
Oops, something went wrong.