Git Product home page Git Product logo

capesepias / play-silhouette-credentials-seed Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adrianhurt/play-silhouette-credentials-seed

0.0 3.0 0.0 31 KB

This Play 2.4 template tries to show you how to secure your application using Silhouette (http://silhouette.mohiva.com/). Please, check the readme file for more details.

License: Other

CoffeeScript 1.18% CSS 0.16% Scala 56.78% HTML 33.17% Erlang 8.71%

play-silhouette-credentials-seed's Introduction

Play Silhouette Credentials Seed [Play 2.4 - Scala]

This project tries to be an example of how to implement an Authentication and Authorization layer using the Silhouette authentication library.

This template only show you how to implement a credential authentication, but it's easy to add the social authentication as well seeing the Silhouette documentation and other templates like these.

It implements the typical authentication and authorization functionality based on roles. You can:

  • Sign up (with email confirmation)
  • Sign in (with remember me)
  • Sign out
  • Change password
  • Reset password (via email)
  • Control of public and private areas
  • Restrict areas to those users whose roles match with the specified ones (with logic OR or AND)

And please, don't forget starring this project if you consider it has been useful for you.

Also check my other projects:

First of all: configure the Mail Plugin

I've used the Mailer plugin to send an email to the user for resetting passwords and email confirmation. For development it's configured to simply print the output to the console (with play.mailer.mock=true configuration). But to send real emails you need to set your smtp parameters in the configuration file.

For example, for a gmail email address:

play.mailer {
  host=smtp.gmail.com
  port=465
  user="[email protected]"
  password=yourpassword
  ssl=true
  from="[email protected]"
  mock=false
}

Silhouette

All the authentication and authorization functionalities are implemented using the Silhouette authentication library. Please check the documentation first.

The main ideas you have to know to understand the code are:

  • Instead of using Dependency Injection for every required Silhouette variable, I've implemented a new superclass of Environment that has everything. Thanks to this other template.
  • I have used some implicit functions to use LoginInfo and PasswordInfo objects as simple Strings and vice versa. It makes clearer the code, but you have to remember that. You can check them at app/utils/silhouette/Implicits.scala.
  • The Auth controller contains every action related with authentication or authorization.

Let's see some interesting files:

  • app/models/User.scala: the user class (with its login info: email and encrypted password). All the users are stored dynamically in a HashMap.
  • app/models/MailTokenUser.scala: implements a token for the web page in case to reset a password or confirm a user's email during a sign up. All the tokens are stored dynamically in a HashMap.
  • app/utils/silhouette/AuthenticationEnvironment.scala: extends the Environment provided by Silhouette adding every required stuff.
  • app/utils/silhouette/AuthenticationController.scala: declares the Controller that gives all the required functionality for Silhouette library.
  • app/utils/silhouette/UserService.scala: simply retrieves a user from its corresponding LoginInfo.
  • app/utils/silhouette/PasswordInfoDAO.scala: simply retrieves and saves a user's PasswordInfo from its corresponding LoginInfo.
  • app/utils/silhouette/MailTokenUserService.scala: implements the corresponding MailTokenService[MailToken].
  • app/utils/silhouette/Authorization.scala: provides the corresponding Authorization classes.
  • app/utils/ErrorHandler.scala: with SecuredErrorHandler.

Authentication

Please, check the Auth controller ( app/controllers/Auth.scala) to know how to:

  • Sign up (with email confirmation)
  • Sign in (with remember me)
  • Sign out
  • Change password
  • Reset password (via email)

Authorization

Each user has one or more services that indicate a specific area or hierarchical level. You can restrict sections to those users who match with a set of services (using logic OR or AND, you can choose). The master role has always full access to everywhere. For example:

  • serviceA: the user has access to the 'service A' area.
  • serviceA and serviceB: the user has access to 'service A' and 'service B' areas.
  • master: full access to every point of the webpage.

The Authorization objects are implemented in app/utils/silhouette/Authorization.scala.

  • WithService(anyOf: String*): you can specifiy a list of services using logic OR. So only those users with any of the specified services will be allowed to access.
  • WithServices(allOf: String*): you can specifiy a list of services using logic AND. So only those users with all of the specified services will be allowed to access.

You also have some tags to customise your UI according to the services for the logged user. They are within the folder app/views/admin/tags/auth.

  • withService(anyOf: String*) { … }
  • withServiceOrElse(anyOf: String*) { … } { … }
  • withServices(anyOf: String*) { … }
  • withServicesOrElse(anyOf: String*) { … } { … }

You will see a bit more information when you sign in and you will be able to try the authorization functionality by yourself.

play-silhouette-credentials-seed's People

Contributors

adrianhurt avatar

Watchers

James Cloos avatar Cape Sepias | Martijn avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.