Commit 2823686137590922453b9f3370d248b04f56a821
1 parent
876e21bf
Exists in
master
FWK-211: Autenticação com Token via TokenAuthenticator e TokenManager
Task-Url: https://demoiselle.atlassian.net/browse/FWK-211
Showing
3 changed files
with
145 additions
and
4 deletions
Show diff stats
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/internal/implementation/MetadataREST.java
@@ -39,7 +39,6 @@ package br.gov.frameworkdemoiselle.internal.implementation; | @@ -39,7 +39,6 @@ package br.gov.frameworkdemoiselle.internal.implementation; | ||
39 | import static javax.ws.rs.core.MediaType.TEXT_HTML; | 39 | import static javax.ws.rs.core.MediaType.TEXT_HTML; |
40 | 40 | ||
41 | import java.util.ResourceBundle; | 41 | import java.util.ResourceBundle; |
42 | -import java.util.logging.Logger; | ||
43 | 42 | ||
44 | import javax.inject.Inject; | 43 | import javax.inject.Inject; |
45 | import javax.ws.rs.GET; | 44 | import javax.ws.rs.GET; |
@@ -55,9 +54,6 @@ import br.gov.frameworkdemoiselle.util.Metadata; | @@ -55,9 +54,6 @@ import br.gov.frameworkdemoiselle.util.Metadata; | ||
55 | public class MetadataREST { | 54 | public class MetadataREST { |
56 | 55 | ||
57 | @Inject | 56 | @Inject |
58 | - private Logger logger; | ||
59 | - | ||
60 | - @Inject | ||
61 | private ResourceBundle bundle; | 57 | private ResourceBundle bundle; |
62 | 58 | ||
63 | @GET | 59 | @GET |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/security/TokenAuthenticator.java
0 → 100644
@@ -0,0 +1,99 @@ | @@ -0,0 +1,99 @@ | ||
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.security; | ||
38 | + | ||
39 | +import static br.gov.frameworkdemoiselle.annotation.Priority.L2_PRIORITY; | ||
40 | + | ||
41 | +import java.security.Principal; | ||
42 | + | ||
43 | +import javax.enterprise.context.RequestScoped; | ||
44 | + | ||
45 | +import br.gov.frameworkdemoiselle.annotation.Priority; | ||
46 | +import br.gov.frameworkdemoiselle.util.Beans; | ||
47 | + | ||
48 | +@RequestScoped | ||
49 | +@Priority(L2_PRIORITY) | ||
50 | +public class TokenAuthenticator implements Authenticator { | ||
51 | + | ||
52 | + private static final long serialVersionUID = 1L; | ||
53 | + | ||
54 | + private Principal user; | ||
55 | + | ||
56 | + @Override | ||
57 | + public void authenticate() throws Exception { | ||
58 | + Token token = Beans.getReference(Token.class); | ||
59 | + TokenManager tokenManager = Beans.getReference(TokenManager.class); | ||
60 | + | ||
61 | + if (token.isEmpty()) { | ||
62 | + this.user = customAuthentication(); | ||
63 | + | ||
64 | + String newToken = tokenManager.persist(this.user); | ||
65 | + token.setValue(newToken); | ||
66 | + | ||
67 | + } else { | ||
68 | + this.user = tokenAuthentication(token, tokenManager); | ||
69 | + } | ||
70 | + } | ||
71 | + | ||
72 | + protected Principal customAuthentication() throws Exception { | ||
73 | + ServletAuthenticator authenticator = Beans.getReference(ServletAuthenticator.class); | ||
74 | + authenticator.authenticate(); | ||
75 | + | ||
76 | + return authenticator.getUser(); | ||
77 | + } | ||
78 | + | ||
79 | + private Principal tokenAuthentication(Token token, TokenManager tokenManager) throws Exception { | ||
80 | + Principal principal = tokenManager.load(token.getValue()); | ||
81 | + | ||
82 | + if (principal == null) { | ||
83 | + throw new InvalidCredentialsException("token inválido"); | ||
84 | + } | ||
85 | + | ||
86 | + return principal; | ||
87 | + } | ||
88 | + | ||
89 | + @Override | ||
90 | + // TODO Apagar o token | ||
91 | + public void unauthenticate() { | ||
92 | + this.user = null; | ||
93 | + } | ||
94 | + | ||
95 | + @Override | ||
96 | + public Principal getUser() { | ||
97 | + return this.user; | ||
98 | + } | ||
99 | +} |
impl/extension/rest/src/main/java/br/gov/frameworkdemoiselle/security/TokenManager.java
0 → 100644
@@ -0,0 +1,46 @@ | @@ -0,0 +1,46 @@ | ||
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.security; | ||
38 | + | ||
39 | +import java.security.Principal; | ||
40 | + | ||
41 | +public interface TokenManager { | ||
42 | + | ||
43 | + String persist(Principal user) throws Exception; | ||
44 | + | ||
45 | + Principal load(String token) throws Exception; | ||
46 | +} |