Intro to Apache Shiro

58
Simple Application Security Les Hazlewood Apache Shiro Project Chair CTO, Stormpath

description

Apache Shiro, a simple easy-to-use framework to enforce user security by Shiro PMC Chair and Stormpath CTO, Les Hazlewood. http://shiro.apache.org http://stormpath.com

Transcript of Intro to Apache Shiro

Page 1: Intro to Apache Shiro

Simple Application SecurityLes Hazlewood

Apache Shiro Project Chair CTO, Stormpath

Page 2: Intro to Apache Shiro

What is Apache Shiro?

• Application security framework

• ASF TLP - http://shiro.apache.org

• Quick and Easy

• Simplifies Security Concepts & Design

Page 3: Intro to Apache Shiro

Agenda

Web Support

Auxiliary Features

Authentication

Session Management

Authorization

Cryptography

IBM_USER
What should come first? Crypto or Sessions?Your old deck has sessions first. If it doesn't matter than lets keep crypto first so i don't have to change the graphics
Page 4: Intro to Apache Shiro

Quick Terminology

• Subject – Security-specific user ‘view’

• Principals – Subject’s identifying attributes

• Credentials – Secret values that verify identity

• Realm – Security-specific DAO

Page 5: Intro to Apache Shiro

Authentication

Web Support

Auxiliary Features

Session Management

AuthorizationAuthentication

Cryptography

Page 6: Intro to Apache Shiro

Authentication Defined

Identity verification:

Proving a user is who he says he is

Page 7: Intro to Apache Shiro

Shiro Authentication Features• Subject-based (current user)

• Single method call

• Rich Exception Hierarchy

• ‘Remember Me’ built in

• Event listeners

Page 8: Intro to Apache Shiro

How to Authenticate with Shiro

Steps

1. Collect principals & credentials

2. Submit to Authentication System

3. Allow, retry, or block access

Page 9: Intro to Apache Shiro

Step 1: Collecting Principals & Credentials

UsernamePasswordToken token = new UsernamePasswordToken(username, password);

//”Remember Me” built-in:token.setRememberMe(true);

IBM_USER
Should code be in bold compared to comments?
Page 10: Intro to Apache Shiro

Step 2: Submission

Subject currentUser = SecurityUtils.getSubject();

currentUser.login(token);

IBM_USER
Font too small. Can we reduce the line count of this code block?
Page 11: Intro to Apache Shiro

Step 3: Grant Access or Handle Failuretry { currentUser.login(token);} catch (UnknownAccountException uae ){ ...} catch (IncorrectCredentialsException ice

{ ...} catch ( LockedAccountException lae ) { ...} catch ( ExcessiveAttemptsException eae )

{ ...} ... catch your own ...} catch ( AuthenticationException ae ) { //unexpected error?}

//No problems, show authenticated view…

IBM_USER
Font too small. Can we reduce the line count of this code block?
Page 12: Intro to Apache Shiro

How does it work?Subject .login(token)

Page 13: Intro to Apache Shiro

How does it work?Subject

SecurityManager

.login(token)

Page 14: Intro to Apache Shiro

How does it work?Subject

SecurityManager

Authenticator

.login(token)

Page 15: Intro to Apache Shiro

How does it work?Subject

SecurityManager

Authenticator

Realm 1

.login(token)

…Realm 2 Realm N

Page 16: Intro to Apache Shiro

How does it work?Subject

SecurityManager

Authenticator

Realm 1

.login(token)

AuthenticationStrategy

…Realm 2 Realm N

Page 17: Intro to Apache Shiro

Authorization

Web Support

Auxiliary Features

Session Management

Authentication Authorization

Cryptography

Page 18: Intro to Apache Shiro

Authorization Defined

Process of determining “who can do what”AKA Access Control

Elements of Authorization• Permissions• Roles• Users

Page 19: Intro to Apache Shiro

Permissions Defined

• Most atomic security element

• Describes resource types and their behavior

• The “what” of an application

• Does not define “who”

• AKA “rights”

Page 20: Intro to Apache Shiro

Roles Defined

• Implicit or Explicit construct

• Implicit: Name only

• Explicit: A named collection of Permissions

Allows behavior aggregation

Enables dynamic (runtime) alteration of user abilities.

IBM_USER
Need to clean up bullets on slides: Implicit/ExplictDoesn't actually define a role
Page 21: Intro to Apache Shiro

Users Defined

• The “who” of the application

• What each user can do is defined by their association with Roles or Permissions

Example: User’s roles imply PrinterPermission

IBM_USER
What's the point of this slide and specifically the second bullet
Page 22: Intro to Apache Shiro

Authorization Features

• Subject-centric (current user)

• Checks based on roles or permissions

• Powerful out-of-the-box WildcardPermission

• Any data model – Realms decide

Page 23: Intro to Apache Shiro

How to Authorize with Shiro

Multiple means of checking access control:

• Programmatically

• JDK 1.5 annotations & AOP

• JSP/GSP/JSF* TagLibs (web support)

Page 24: Intro to Apache Shiro

Programmatic Authorization

//get the current SubjectSubject currentUser = SecurityUtils.getSubject();

if (currentUser.hasRole(“administrator”)) { //show the ‘delete user’ button} else { //don’t show the button?)}

Role Check

Page 25: Intro to Apache Shiro

Programmatic Authorization

Subject currentUser = SecurityUtils.getSubject();

Permission deleteUser = new UserPermission(“jsmith”,“delete”);

If (currentUser.isPermitted(deleteUser)) { //show the ‘delete user’ button} else { //don’t show the button?}

Permission Check

Page 26: Intro to Apache Shiro

Programmatic Authorization

String perm = “user:delete:jsmith”;

if(currentUser.isPermitted(perm)){ //show the ‘delete user’ button} else { //don’t show the button?}

Permission Check (String-based)

Page 27: Intro to Apache Shiro

Annotation Authorization

@RequiresRoles( “teller” )public void openAccount(Account a) { //do something in here that //only a ‘teller’ should do}

Role Check

Page 28: Intro to Apache Shiro

Annotation Authorization

@RequiresPermissions(“account:create”)public void openAccount(Account a) { //create the account}

Permission Check

Page 29: Intro to Apache Shiro

Enterprise Session Management

Web Support

Auxiliary Features

AuthorizationAuthentication

CryptographySession Management

Page 30: Intro to Apache Shiro

Session Management Defined

Managing the lifecycle of Subject-specific temporal data context

Page 31: Intro to Apache Shiro

Session Management Features

• Heterogeneous client access• POJO/J2SE based (IoC friendly)• Event listeners• Host address retention• Inactivity/expiration support (touch())• Transparent web use - HttpSession• Container-Independent Clustering!

Page 32: Intro to Apache Shiro

Acquiring and Creating SessionsSubject currentUser =

SecurityUtils.getSubject()

//guarantee a session

Session session = subject.getSession();

//get a session if it exists

subject.getSession(false);

Page 33: Intro to Apache Shiro

Session API

getStartTimestamp()

getLastAccessTime()

getAttribute(key)

setAttribute(key, value)

get/setTimeout(long)

touch()

...

Page 34: Intro to Apache Shiro

Cryptography

Web Support

Auxiliary Features

AuthorizationAuthentication

Session Management Cryptography

Page 35: Intro to Apache Shiro

Cryptography Defined

Protecting information from undesired access by hiding it or converting it into nonsense.

Elements of Cryptography• Ciphers

• Hashes

Page 36: Intro to Apache Shiro

Ciphers Defined

Encryption and decryption data based on shared or public/private keys.

• Symmetric Cipher – same key• Block Cipher – chunks of bits• Stream Cipher – stream of bits

• Asymmetric Cipher - different keys

Page 37: Intro to Apache Shiro

Hashes Defined

A one-way, irreversible conversion of an input source (a.k.a. Message Digest)

Used for:• Credentials transformation, Checksum

• Data with underlying byte arrayFiles, Streams, etc

IBM_USER
Not parrallel to ciphers defined slide
Page 38: Intro to Apache Shiro

Cryptography Features

Simplicity• Interface-driven, POJO based

• Simplified wrapper over JCE infrastructure.

• “Object Orientifies” cryptography concepts

• Easier to understand API

Page 39: Intro to Apache Shiro

Cipher Features

• OO Hierarchy JcaCipherService, AbstractSymmetricCipherService,

DefaultBlockCipherService, etc

• Just instantiate a classNo “Transformation String”/Factory methods

• More secure default settings than JDK!Cipher Modes, Initialization Vectors, et. al.

Page 40: Intro to Apache Shiro

Example: Plaintext

(image courtesy WikiPedia)

Page 41: Intro to Apache Shiro

Example: ECB Mode (JDK Default!)

(image courtesy WikiPedia)

Page 42: Intro to Apache Shiro

Example: Shiro Defaults

(image courtesy WikiPedia)

Page 43: Intro to Apache Shiro

Shiro’s CipherService Interfacepublic interface CipherService {

ByteSource encrypt(byte[] raw, byte[] key);

void encrypt(InputStream in, OutputStream out, byte[] key);

ByteSource decrypt( byte[] cipherText, byte[] key);

void decrypt(InputStream in, OutputStream out, byte[] key); }

Page 44: Intro to Apache Shiro

Hash Features

• Default interface implementationsMD5, SHA1, SHA-256, et. al.

• Built in Hex & Base64 conversion

• Built-in support for Salts and repeated hashing

IBM_USER
Needs clean up. Fewer points or consolidation of points.Point 1, 2, and 5 are of note
Page 45: Intro to Apache Shiro

Shiro’s Hash Interface

public interface Hash {

byte[] getBytes();

String toHex();

String toBase64();

}

Page 46: Intro to Apache Shiro

Intuitive OO Hash API//some examples:new Md5Hash(“foo”).toHex();

//File MD5 Hash value for checksum:new Md5Hash( aFile ).toHex();

//store password, but not plaintext:new Sha512(aPassword, salt, 1024).toBase64();

Page 47: Intro to Apache Shiro

Web Support

Web Support

Auxiliary Features

Cryptography Session Management

AuthorizationAuthentication

Page 48: Intro to Apache Shiro

Web Support Features

• Simple ShiroFilter web.xml definition

• Protects all URLs

• Innovative Filtering (URL-specific chains)

• JSP Tag support

• Transparent HttpSession support

Page 49: Intro to Apache Shiro

web.xml<filter> <filter-name>ShiroFilter</filter-name> <filter-class> org.apache.shiro.web.servlet.IniShiroFilter </filter-class></filter>

<filter-mapping> <filter-name>ShiroFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>

Page 50: Intro to Apache Shiro

shiro.ini[main]ldapRealm = org.apache.shiro.realm.ldap.JndiLdapRealmldapRealm.userDnTemplate = uid={0},ou=users,dc=mycompany,dc=comldapRealm.contextFactory.url = ldap://ldapHost:389

securityManager.realm = $realm [urls]/images/** = anon/account/** = authc/rest/** = authcBasic/remoting/** = authc, roles[b2bClient], …

Page 51: Intro to Apache Shiro

JSP TagLib Authorization<%@ taglib prefix=“shiro” uri=“http://shiro.apache.org/tags” %><html><body> <shiro:hasRole name=“administrator”> <a href=“manageUsers.jsp”> Click here to manage users </a> </shiro:hasRole> <shiro:lacksRole name=“administrator”> No user admin for you! </shiro:hasRole></body></html>

Page 52: Intro to Apache Shiro

JSP TagLibs<%@ taglib prefix=“shiro” uri=http://shiro.apache.org/tags %>

<!-- Other tags: --><shiro:guest/><shiro:user/><shiro:principal/><shiro:hasRole/><shiro:lacksRole/><shiro:hasAnyRoles/><shiro:hasPermission/><shiro:lacksPermission/><shiro:authenticated/><shiro:notAuthenticated/>

Page 53: Intro to Apache Shiro

Auxiliary Features

Web Support

Auxiliary Features

Cryptography Session Management

AuthorizationAuthentication

Page 54: Intro to Apache Shiro

Auxiliary Features

• Threading & Concurrency

Callable/Runnable & Executor/ExecutorService

• “Run As” support

• Ad-hoc Subject instance creation

• Unit Testing

• Remembered vs Authenticated

Page 55: Intro to Apache Shiro

Logging Out

One method:

App-specific log-out logic:

Before/After the call

Listen for Authentication or StoppedSession events.

//Logs the user out, relinquishes account //data, and invalidates any SessionSecurityUtils.getSubject().logout();

IBM_USER
Lines need to be cleaned up. Too many carraige returns.
Page 56: Intro to Apache Shiro

Application + Stormpath

Realm

Stormpath: Application Security Service

Out-of-the-box Features• Managed security data model• Secure credential storage• Flexible permissions• Password self-service GUI• Management GUI

StormpathAuthentication Access Control

• Realms + Plug-ins• REST API

Page 57: Intro to Apache Shiro

Public Cloud

Stormpath: Cloud Deployment

Application

Application

Application

Active Directory

Corporate Network

Fire

wal

l

Outbound Sync

SAML

REST

StormpathOpenId/OAuth

Page 58: Intro to Apache Shiro

Thank You!

[email protected]• http://www.stormpath.com

• Seeking engineering talent

• Seeking product feedback