PageRenderTime 28ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/AuthenticRoast/AuthenticRoast-Samples/src/main/java/name/aikesommer/authenticator/samples/FormAndTicketSample.java

http://authenticroast.googlecode.com/
Java | 91 lines | 39 code | 15 blank | 37 comment | 0 complexity | 611ab78cdb69ec27907b163dfa94b369 MD5 | raw file
 1/**
 2 *    Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
 3 *
 4 *    This file is part of AuthenticRoast.
 5 *
 6 *    This file is just a sample. You are free to use and modify the
 7 *    code in this file to your liking!
 8 *
 9 *    This file is distributed in the hope that it will be useful,
10 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 *    You can reach the author and get more information about this
14 *    project at: http://aikesommer.name/
15 */
16package name.aikesommer.authenticator.samples;
17
18import java.util.ArrayList;
19import java.util.Collection;
20import java.util.List;
21import name.aikesommer.authenticator.AuthenticationRequest;
22import name.aikesommer.authenticator.CompositeAuthenticator;
23import name.aikesommer.authenticator.FormAuthenticator;
24import name.aikesommer.authenticator.LogoutManager;
25import name.aikesommer.authenticator.PluggableAuthenticator;
26import name.aikesommer.authenticator.SimplePrincipal;
27import name.aikesommer.authenticator.TicketAuthenticator;
28
29/**
30 * This is just a sample, make sure to actually check credentials and such!!
31 * 
32 * @author Aike J Sommer
33 */
34public class FormAndTicketSample extends CompositeAuthenticator {
35
36    @Override
37    protected Collection<PluggableAuthenticator> createAuthenticators() {
38        List<PluggableAuthenticator> result = new ArrayList();
39        
40        /**
41         * Allow form-based logins.
42         */
43        result.add(new FormAuthenticator() {
44
45            @Override
46            protected boolean checkCredentials(AuthenticationManager manager, AuthenticationRequest request, String username, String password) {
47                // check the credentials with some config-files, db-data
48                // or a realm in the app-server
49                
50                // we just return true here, so everything will be accepted
51                return true;
52            }
53
54            @Override
55            protected SimplePrincipal loadPrincipal(AuthenticationManager manager, AuthenticationRequest request, String username) {
56                // load user-data from config-files, db or where ever you
57                // have it stored :-)
58                
59                return new SimplePrincipal(username, "user");
60            }
61        });
62        
63        /**
64         * Allow ticket-based logins.
65         */
66        result.add(new TicketAuthenticator() {
67
68            @Override
69            protected boolean checkTicket(AuthenticationManager manager, AuthenticationRequest request, String ticket) {
70                // check the ticket, for example with some secret and a 
71                // hash
72                
73                // we just return true here, so everything will be accepted
74                return true;
75            }
76
77            @Override
78            protected SimplePrincipal loadPrincipal(AuthenticationManager manager, AuthenticationRequest request, String ticket) {
79                return new SimplePrincipal("guest", "guest");
80            }
81        });
82        
83        /**
84         * Allow a user to "logout".
85         */
86        result.add(new LogoutManager());
87        
88        return result;
89    }
90
91}