| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TokenFromSpaceAuthenticator |
|
| 2.5;2.5 |
| 1 | /* | |
| 2 | * ============================================================================ | |
| 3 | * | |
| 4 | * File: TokenFromSpaceAuthenticator.java | |
| 5 | *---------------------------------------------------------------------------- | |
| 6 | * | |
| 7 | * Copyright 2008 Erlend Nossum | |
| 8 | * | |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 10 | * you may not use this file except in compliance with the License. | |
| 11 | * You may obtain a copy of the License at | |
| 12 | * | |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 14 | * | |
| 15 | * Unless required by applicable law or agreed to in writing, software | |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 18 | * See the License for the specific language governing permissions and | |
| 19 | * limitations under the License. | |
| 20 | * | |
| 21 | * Description: See javadoc below | |
| 22 | * | |
| 23 | * Created: Mar 16, 2008 | |
| 24 | * ============================================================================ | |
| 25 | */ | |
| 26 | ||
| 27 | package org.semispace.ws; | |
| 28 | ||
| 29 | import java.util.Random; | |
| 30 | ||
| 31 | import org.slf4j.Logger; | |
| 32 | import org.slf4j.LoggerFactory; | |
| 33 | import org.semispace.NameValueQuery; | |
| 34 | import org.semispace.SemiSpaceInterface; | |
| 35 | ||
| 36 | /** | |
| 37 | * Query on simple name / value elements read from | |
| 38 | * space. This implementation exist as an <b>example</b> | |
| 39 | * and for the benefit of junit tests. You will want | |
| 40 | * to implement your own version in a live situation. | |
| 41 | * | |
| 42 | * <p>See method doc for more information.</p> | |
| 43 | * | |
| 44 | * @see NameValueQuery | |
| 45 | */ | |
| 46 | 0 | public class TokenFromSpaceAuthenticator implements TokenAuthenticator { |
| 47 | 0 | private static final Logger log = LoggerFactory.getLogger(TokenFromSpaceAuthenticator.class); |
| 48 | /** | |
| 49 | * Default session time of 30 minutes. | |
| 50 | */ | |
| 51 | private static final long DEFAULT_SESSION_LENGTH = 1000*60*30; | |
| 52 | private SemiSpaceInterface space; | |
| 53 | ||
| 54 | /** For the benefit of spring */ | |
| 55 | public void setSpace(SemiSpaceInterface space) { | |
| 56 | 0 | this.space = space; |
| 57 | 0 | } |
| 58 | ||
| 59 | /** | |
| 60 | * As we use NameValueQuery from the semispace-main, we | |
| 61 | * prepend the name field with <code>authname=</code> in order to | |
| 62 | * avoid name clashes. | |
| 63 | * @see org.semispace.ws.TokenAuthenticator#authenticate(java.lang.String, java.lang.String) | |
| 64 | */ | |
| 65 | public String authenticate(String username, String password) { | |
| 66 | 0 | NameValueQuery nvq = new NameValueQuery(); |
| 67 | 0 | nvq.name = "authname="+username; |
| 68 | 0 | nvq.value = password; |
| 69 | 0 | NameValueQuery result = (NameValueQuery) space.readIfExists(nvq); |
| 70 | 0 | if ( result != null ) { |
| 71 | // User exist in space | |
| 72 | 0 | NameValueQuery tokenQuery = new NameValueQuery(); |
| 73 | 0 | tokenQuery.value = "tokenFor="+username; |
| 74 | // Remove potentially existing tokens | |
| 75 | 0 | for ( NameValueQuery existing = tokenQuery ; existing != null ; existing = (NameValueQuery) space.takeIfExists(tokenQuery) ) { |
| 76 | // Intentional | |
| 77 | } | |
| 78 | 0 | String token = generateToken(); |
| 79 | 0 | tokenQuery.name = "token="+token; |
| 80 | 0 | space.write(tokenQuery, DEFAULT_SESSION_LENGTH); |
| 81 | 0 | return token; |
| 82 | } | |
| 83 | ||
| 84 | 0 | return null; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Generate a random token. | |
| 89 | */ | |
| 90 | private String generateToken() { | |
| 91 | 0 | final Random wheel = new Random(); |
| 92 | 0 | String rnd = Long.toString( wheel.nextLong() & Long.MAX_VALUE , 36 ); |
| 93 | 0 | if ( rnd.length() > 7 ) { |
| 94 | 0 | rnd = rnd.substring(0,7); |
| 95 | } | |
| 96 | 0 | return rnd; |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Side effect: The token life was renewed | |
| 101 | * @return If the token was found to be valid | |
| 102 | * @see org.semispace.ws.TokenAuthenticator#isTokenValid(java.lang.String) | |
| 103 | */ | |
| 104 | public boolean isTokenValid(String token) { | |
| 105 | 0 | NameValueQuery tokenQuery = new NameValueQuery(); |
| 106 | 0 | tokenQuery.name = "token="+token; |
| 107 | 0 | NameValueQuery spaceToken = (NameValueQuery) space.take(tokenQuery, 200); |
| 108 | 0 | if ( spaceToken != null ) { |
| 109 | // This is in order to renew session | |
| 110 | 0 | space.write(spaceToken, DEFAULT_SESSION_LENGTH); |
| 111 | 0 | return true; |
| 112 | } | |
| 113 | 0 | return false; |
| 114 | } | |
| 115 | ||
| 116 | } |