]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org/argeo/osgi/useradmin/TokenUtils.java
1 package org
.argeo
.osgi
.useradmin
;
3 import static org
.argeo
.util
.naming
.LdapAttrs
.description
;
4 import static org
.argeo
.util
.naming
.LdapAttrs
.owner
;
6 import java
.security
.Principal
;
7 import java
.time
.Instant
;
8 import java
.util
.HashSet
;
11 import javax
.naming
.InvalidNameException
;
12 import javax
.naming
.ldap
.LdapName
;
13 import javax
.security
.auth
.Subject
;
15 import org
.argeo
.util
.naming
.NamingUtils
;
16 import org
.osgi
.service
.useradmin
.Group
;
19 * Canonically implements the Argeo token conventions.
21 public class TokenUtils
{
22 public static Set
<String
> tokensUsed(Subject subject
, String tokensBaseDn
) {
23 Set
<String
> res
= new HashSet
<>();
24 for (Principal principal
: subject
.getPrincipals()) {
25 String name
= principal
.getName();
26 if (name
.endsWith(tokensBaseDn
)) {
28 LdapName ldapName
= new LdapName(name
);
29 String token
= ldapName
.getRdn(ldapName
.size()).getValue().toString();
31 } catch (InvalidNameException e
) {
32 throw new UserDirectoryException("Invalid principal " + principal
, e
);
39 /** The user related to this token group */
40 public static String
userDn(Group tokenGroup
) {
41 return (String
) tokenGroup
.getProperties().get(owner
.name());
44 public static boolean isExpired(Group tokenGroup
) {
45 return isExpired(tokenGroup
, Instant
.now());
49 public static boolean isExpired(Group tokenGroup
, Instant instant
) {
50 String expiryDateStr
= (String
) tokenGroup
.getProperties().get(description
.name());
51 if (expiryDateStr
!= null) {
52 Instant expiryDate
= NamingUtils
.ldapDateToInstant(expiryDateStr
);
53 if (expiryDate
.isBefore(instant
)) {
60 // private final String token;
62 // public TokenUtils(String token) {
63 // this.token = token;
66 // public String getToken() {
71 // public int hashCode() {
72 // return token.hashCode();
76 // public boolean equals(Object obj) {
77 // if ((obj instanceof TokenUtils) && ((TokenUtils) obj).token.equals(token))
83 // public String toString() {
84 // return "Token #" + hashCode();