Commit 735de5a44f56e89b5de20b813e82fedb2322cd1a
1 parent
28236861
Exists in
master
FWK-213: Implementação padrão do TokenManager em @ApplicationScoped (não
recomendado em produção) Task-Url: https://demoiselle.atlassian.net/browse/FWK-213
Showing
1 changed file
with
91 additions
and
0 deletions
Show diff stats
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/DefaultTokenManager.java
0 → 100644
| @@ -0,0 +1,91 @@ | @@ -0,0 +1,91 @@ | ||
| 1 | +/* | ||
| 2 | + * Demoiselle Framework | ||
| 3 | + * Copyright (C) 2010 SERPRO | ||
| 4 | + * ---------------------------------------------------------------------------- | ||
| 5 | + * This file is part of Demoiselle Framework. | ||
| 6 | + * | ||
| 7 | + * Demoiselle Framework is free software; you can redistribute it and/or | ||
| 8 | + * modify it under the terms of the GNU Lesser General Public License version 3 | ||
| 9 | + * as published by the Free Software Foundation. | ||
| 10 | + * | ||
| 11 | + * This program is distributed in the hope that it will be useful, | ||
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | + * GNU General Public License for more details. | ||
| 15 | + * | ||
| 16 | + * You should have received a copy of the GNU Lesser General Public License version 3 | ||
| 17 | + * along with this program; if not, see <http://www.gnu.org/licenses/> | ||
| 18 | + * or write to the Free Software Foundation, Inc., 51 Franklin Street, | ||
| 19 | + * Fifth Floor, Boston, MA 02110-1301, USA. | ||
| 20 | + * ---------------------------------------------------------------------------- | ||
| 21 | + * Este arquivo é parte do Framework Demoiselle. | ||
| 22 | + * | ||
| 23 | + * O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou | ||
| 24 | + * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação | ||
| 25 | + * do Software Livre (FSF). | ||
| 26 | + * | ||
| 27 | + * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA | ||
| 28 | + * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou | ||
| 29 | + * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português | ||
| 30 | + * para maiores detalhes. | ||
| 31 | + * | ||
| 32 | + * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título | ||
| 33 | + * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/> | ||
| 34 | + * ou escreva para a Fundação do Software Livre (FSF) Inc., | ||
| 35 | + * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | ||
| 36 | + */ | ||
| 37 | +package br.gov.frameworkdemoiselle.internal.implementation; | ||
| 38 | + | ||
| 39 | +import static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY; | ||
| 40 | + | ||
| 41 | +import java.io.Serializable; | ||
| 42 | +import java.security.Principal; | ||
| 43 | +import java.util.Collections; | ||
| 44 | +import java.util.HashMap; | ||
| 45 | +import java.util.Map; | ||
| 46 | +import java.util.UUID; | ||
| 47 | + | ||
| 48 | +import javax.enterprise.context.ApplicationScoped; | ||
| 49 | + | ||
| 50 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
| 51 | +import br.gov.frameworkdemoiselle.security.TokenManager; | ||
| 52 | + | ||
| 53 | +@ApplicationScoped | ||
| 54 | +@Priority(L2_PRIORITY) | ||
| 55 | +public class DefaultTokenManager implements TokenManager { | ||
| 56 | + | ||
| 57 | + private TokenStore store = new TokenStore(); | ||
| 58 | + | ||
| 59 | + @Override | ||
| 60 | + public String persist(Principal user) throws Exception { | ||
| 61 | + return store.put(user); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public Principal load(String token) throws Exception { | ||
| 66 | + return store.get(token); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + private class TokenStore implements Serializable { | ||
| 70 | + | ||
| 71 | + private static final long serialVersionUID = 1L; | ||
| 72 | + | ||
| 73 | + private Map<String, Principal> map = Collections.synchronizedMap(new HashMap<String, Principal>()); | ||
| 74 | + | ||
| 75 | + public String put(Principal user) { | ||
| 76 | + String token = UUID.randomUUID().toString(); | ||
| 77 | + | ||
| 78 | + if (map.containsValue(user)) { | ||
| 79 | + map.remove(token); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + map.put(token, user); | ||
| 83 | + | ||
| 84 | + return token; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + public Principal get(String token) { | ||
| 88 | + return map.get(token); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | +} |