Developing With JAAS


Published on

Published in: Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • LoginModule never gets called directly. Sun provides a few default LoginModule implementations such as the JNDILoginModule etc.
  • Maybe I should add another slide here to illustrate everything that happens when login() is called. This method is a little confusing at first because it results in the callbacks to be called, and that has a asynchronous feel to it (like one hand is acting independently of the other – one hand does not know what the other is doing).
  • Developing With JAAS

    1. 1. <ul><li>Implementing Java Security with </li></ul><ul><li>JAAS </li></ul><ul><li> Rizwan Ahmed </li></ul><ul><li>© 2003 </li></ul>Implementing Java Security with JAAS Rizwan Ahmed Sun Certified Professional
    2. 2. What is JAAS? <ul><li>Java Authentication and Authorization Service </li></ul><ul><li>Introduced as an optional package in J2SE 1.3 </li></ul><ul><li>Integrated into J2SE 1.4 </li></ul><ul><li>Provides a pluggable and flexible framework that allows developers to incorporate different security mechanisms and sources </li></ul>
    3. 3. Goals <ul><li>JAAS is an extensive and complicated library. </li></ul><ul><li>The goals for this presentation are: </li></ul><ul><ul><li>To give an overview of JAAS and its constituent parts and illustrate how they all work together </li></ul></ul><ul><ul><li>Provide an introduction on how to use JAAS; code examples to help you get started </li></ul></ul>
    4. 4. Outline <ul><li>Introduction </li></ul><ul><ul><li>What is JAAS </li></ul></ul><ul><ul><li>Authentication vs. Authorization </li></ul></ul><ul><ul><li>Subject </li></ul></ul><ul><ul><li>Principal </li></ul></ul><ul><li>Authentication </li></ul><ul><li>Authorization </li></ul>
    5. 5. Authentication vs. Authorization <ul><li>Authentication is the process of verifying the users’ identity. Typically this entails obtaining a user name and a password or some other credential from the user. </li></ul><ul><li>Authorization is the process of verifying whether a user has access to protected resources. </li></ul>
    6. 6. Overview of the Subject <ul><li>The Subject is a container for associated Principals, Public Credentials (public keys), and Private Credentials (passwords, private keys). </li></ul>
    7. 7. Subject <ul><li>A Subject in JAAS represents an authenticated entity such as a person or device </li></ul>
    8. 8. Principal <ul><li>A Principal identifies a Subject. The Subject can be a person, a corporation, and application, etc. </li></ul><ul><li>A Subject is comprised of a set of Principals, where each Principal represents an identity for that user. For example, a Subject could have a name Principal (&quot;Susan Smith&quot;) and a Social Security Number Principal (&quot;987-65-4321&quot;), thereby distinguishing this Subject from other Subjects. </li></ul>
    9. 9. Obtaining a Specific Principal from a Subject <ul><li>Applications (and app. servers) typically adopt a convention stating that the Set of Principals on a Subject can only contain one instance of a particular Principal class. </li></ul>
    10. 10. Outline <ul><li>Introduction </li></ul><ul><li>Authentication </li></ul><ul><li>Authorization </li></ul>
    11. 11. Authentication Overview <ul><li>The application creates a LoginContext and calls login() </li></ul><ul><li>The LoginContext looks up the LoginConfiguration to load the predefined LoginModule </li></ul><ul><li>The LoginContext delegates the authentication to the LoginModules </li></ul><ul><li>The LoginModules use the CallbackHandler to communicate with the application </li></ul><ul><li>Associate principals and credentials with the Subject if user is authenticated </li></ul><ul><li>Or, throw a LoginException in case the login failed </li></ul>
    12. 12. Pluggable Authentication Modules <ul><li>The LoginModule provides authentication via a pluggable module, which implements the authentication algorithm and determines how the authentication is performed </li></ul>
    13. 13. LoginConfiguration File <ul><li>The default implementation parses a configuration file in the above format </li></ul><ul><li>Configuration file specified via System parameter </li></ul>
    14. 14. Creating a LoginContext <ul><li>The name parameter is used to retrieve the appropriate login Configuration </li></ul><ul><li>If a login Configuration with the specified name is not found, a default entry with the name “ other ” is used. If there is no Configuration with the name “ other ”, a LoginException is thrown </li></ul>
    15. 15. Creating a LoginContext (cont.) <ul><li>The constructors without a CallbackHandler either: </li></ul><ul><ul><li>Rely on the default CallbackHandler specified in the file under property named auth.login.defaultCallbackHandler </li></ul></ul><ul><ul><li>Do not use a CallbackHandler and rely on the LoginModule s to have another means of obtaining the information </li></ul></ul>
    16. 16. Logging In <ul><li>Authentication occurs with a call to the login() method </li></ul><ul><li>The login() method invokes the configured LoginModules to perform authentication </li></ul><ul><li>When authentication succeeds, the Subject can be retrieved using the getSubject() method </li></ul><ul><li>The logout() method logs out the Subject and removes its authenticated Principal s </li></ul>
    17. 17. LoginModule <ul><li>Two-phase authentication: </li></ul><ul><ul><li>login() is 1 st phase method </li></ul></ul><ul><ul><li>commit() and abort() are 2 nd phase methods </li></ul></ul>
    18. 18. Login Example: Login Phase
    19. 19. Login Example: Commit Phase <ul><li>Once the login succeeds we can get the Subject from the LoginContext and get the authenticated Principals from the Subject. </li></ul>
    20. 20. LoginModules in J2SE 1.4 <ul><li>These exist under </li></ul><ul><li>JndiLoginModule – Authenticates against an LDAP tree </li></ul><ul><li>Krb5LoginModule – Authenticates against a Kerberos domain </li></ul><ul><li>UnixLoginModule – Authenticates against Unix security </li></ul><ul><li>NTLoginModule – Authenticates against NT security </li></ul>
    21. 21. Callbacks <ul><li> Marker interface used to indicate a callback. </li></ul><ul><li>Callbacks provide a means for the underlying authentication implementation to communicate with the application and obtains security data such as user name and password as well as provide information such as error and warning messages. </li></ul><ul><li>Included callbacks: </li></ul><ul><ul><li>NameCallback </li></ul></ul><ul><ul><li>PasswordCallback </li></ul></ul><ul><ul><li>TextOutputCallback </li></ul></ul><ul><ul><li>TextInputCallback </li></ul></ul><ul><ul><li>ChoiceCallback </li></ul></ul><ul><ul><li>ConfirmationCallback </li></ul></ul><ul><ul><li>LanguageCallback </li></ul></ul>
    22. 22. Custom LoginModule Example
    23. 23. CallbackHandler
    24. 24. Custom CallbackHandler Example
    25. 25. Outline <ul><li>Introduction </li></ul><ul><li>Authentication </li></ul><ul><li>Authorization </li></ul>
    26. 26. What is Authorization? <ul><li>Authorization is the process of determining whether an authenticated user is permitted to perform some actions such as accessing a resource. </li></ul><ul><li>The process is Policy based since JAAS is built on the existing Java security model. </li></ul>
    27. 27. CodeSource Based Authorization <ul><li>Before the integration of JAAS with Java security, authorization decisions were strictly based on the CodeSource </li></ul><ul><li>A Trusted Library may be given access to sensitive resources while an Applet or another Library may have that access restricted. </li></ul>
    28. 28. CodeSource and Principal Based Authorization <ul><li>With the integration of JAAS and J2SE Security, authorization decisions can be made based on the CodeSource and the Principal. </li></ul><ul><li>A Library may not have access privileges to resources when running without a User context or when being executed by User Bart, but when User Andy executes the Library those permissions may be granted. </li></ul>
    29. 29. CodeSource & ProtectionDomain <ul><li>The CodeSource of a piece of Java code is the URL location that the code was loaded from and the Certificates that we used to sign the code </li></ul><ul><li>The ProtectionDomain is a holder for the CodeSource and a Principal </li></ul><ul><li>Each class is assigned a ProtectionDomain upon being loaded. The Principal is null when the class is first loaded. </li></ul>
    30. 30. AccessControlContext – a Context for Authorization Decisions <ul><li>When making access decisions, the security system looks at every ProtectionDomain involved in the call. Access is granted only if every ProtectionDomain in the Context can have access. </li></ul>grant codebase &quot;file:./SampleAzn.jar&quot; { permission &quot;createLoginContext.Sample&quot;; permission &quot;doAsPrivileged&quot;; };
    31. 31. How is JAAS Authorization performed? <ul><li>To make JAAS authorization take place, the following is required: </li></ul><ul><li>The user must be authenticated </li></ul><ul><li>Principal-based entries must be configured in the security policy. </li></ul><ul><li>The Subject that is the result of authentication must be associated with the current access control context. </li></ul>
    32. 32. Permission <ul><li>Permissions represent access to resources. </li></ul><ul><li>All Permission object have a name. The meaning of the name parameter is implementation dependent. Typically the name identifies the resource to be accessed. </li></ul>
    33. 33. Policy <ul><li>The mapping between PDs and associated Permissions is stored by the Policy </li></ul><ul><li>The grant entry includes all the permissions granted for the principals to do the security sensitive operations, for example, accessing a particular web page or file </li></ul>
    34. 34. Policy Implementation <ul><li>The default implementation of Policy accepts text based configuration in the above format </li></ul><ul><li>Each grant entry is composed of an optional CodeSource, Signers, Principals, and a list of Permissions </li></ul><ul><li>Default security policy is <JRE_HOME>/lib/security/java.policy </li></ul><ul><li>Can provide supplemental policy file location via </li></ul><ul><ul><li><file> JVM parameter </li></ul></ul><ul><li>Can override the default policy file with: </li></ul><ul><ul><li><file> JVM parameter </li></ul></ul>
    35. 35. AccessController <ul><li>The AccessController embodies the access control algorithm. </li></ul><ul><li>It obtains the current AccessControlContext, which has an array of PDs and then for each PD checks whether the Subject has the requested permission. </li></ul>
    36. 36. Associating a Subject with an Access Control Context <ul><li>The doAs method associates the provided Subject with the current access control context and then invokes the run method from the action. The run method implementation contains all the code to be executed as the specified Subject. The action thus executes as the specified Subject. </li></ul>
    37. 37. Execution flow <ul><li>Invoke Subject.doAs(..) or Subject.doAsPriviledged() </li></ul><ul><li>SecurityManager.checkPermission(..) is called to check the permission </li></ul><ul><li>The SecurityManager delegates the check to the AccessController </li></ul><ul><li>The AccessController tries to find out if the relevant AccessControlContext contains sufficient permissions for the action to be taken </li></ul><ul><li>If not, the SecurityManager updates the current AccessControlContext with the permissions granted to the subject via the Policy from the Policy file </li></ul>
    38. 38. Execution flow PrivilegedAction action = new SampleAction(); // The call to Subject.doAsPrivileged is performed via: Subject.doAsPrivileged(mySubject, action, null); public class SampleAction implements PrivilegedAction { public Object run() { System.out.println(&quot; Your java.home property value is: &quot; + System.getProperty(&quot;java.home&quot;)); System.out.println(&quot; Your user.home property value is: &quot; + System.getProperty(&quot;user.home&quot;)); File f = new File(&quot;foo.txt&quot;); System.out.print(&quot; foo.txt does &quot;); if (!f.exists()) System.out.print(&quot;not &quot;); System.out.println(&quot;exist in the current working directory.&quot;); return null; }}
    39. 39. Custom Policy <ul><li>Like the LoginModule, the Policy is also a pluggable module. You can hook up other Policy implementations by one of these means: </li></ul><ul><ul><li>At runtime by calling Policy.setPolicy() </li></ul></ul><ul><ul><li>By changing the value of policy.provider property in <JRE_HOME>/lib/security/ </li></ul></ul><ul><ul><ul><li>The specified class name must point to a subclass of Policy </li></ul></ul></ul><ul><ul><ul><li>The specified class must be in the boot classpath </li></ul></ul></ul><ul><ul><ul><li>Example included (you must change the property on your JRE) </li></ul></ul></ul>
    40. 40. Extend JAAS <ul><li>JAAS is built on top of the existing Java security model, which is CodeSource based, and the plaintext format policy implementation. </li></ul><ul><li>For an enterprise application you may want to use custom security repositories with JAAS, such as LDAP, database or another file system </li></ul><ul><li>This can be done by writing a customized module thanks to the JAAS pluggable feature </li></ul>
    41. 41. About the Speaker <ul><li>Speaker is a Senior Architect at Ventyx Inc. He has over 5 years of experience in J2EE/Web development and has worked with extensive open source technologies. His interests are in the areas of technical architecture, enterprise application integration and web services. </li></ul>