KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > user > DefaultAuthenticationService


1 package org.snipsnap.user;
2
3 import org.snipsnap.snip.storage.UserStorage;
4
5 /*
6  * This file is part of "SnipSnap Wiki/Weblog".
7  *
8  * Copyright (c) 2002-2003 Stephan J. Schmidt, Matthias L. Jugel
9  * All Rights Reserved.
10  *
11  * Please visit http://snipsnap.org/ for updates and contact.
12  *
13  * --LICENSE NOTICE--
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free SoftSware Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  * --LICENSE NOTICE--
28  */

29
30 public class DefaultAuthenticationService implements AuthenticationService {
31   private UserStorage storage;
32
33   public DefaultAuthenticationService(UserStorage storage) {
34     this.storage = storage;
35   }
36
37   public User authenticate(String JavaDoc login, String JavaDoc passwd) {
38     return authenticate(login, passwd, !ENCRYPTED);
39   }
40
41   public User authenticate(String JavaDoc login, String JavaDoc passwd, boolean encrypted) {
42     User user = storage.storageLoad(login);
43
44 // System.out.println("user: "+user);
45
// System.out.println("check: unencrypted: "+user.getPasswd().equals(passwd));
46
// System.out.println(passwd+"-"+Digest.getDigest(passwd)+"-"+user.getPasswd());
47
// System.out.println("check: encrypted: "+Digest.authenticate(passwd, user.getPasswd()));
48

49     //@TODO split authenticate and lastLogin
50
if (null != user &&
51       (encrypted ? user.getPasswd().equals(passwd) : Digest.authenticate(passwd, user.getPasswd()))) {
52       user.lastLogin();
53       storage.storageStore(user);
54       return user;
55     } else {
56       return null;
57     }
58   }
59
60   public boolean isAuthenticated(User user) {
61     return user != null && !(user.isGuest() || user.isNonUser());
62   }
63 }
64
Popular Tags