aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/main/java/org/opendaylight/aaa/authn/mdsal/store/util/AuthNStoreUtil.java
blob: 6ef58109f31f3fe2189745c6e7adc506561c9187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.authn.mdsal.store.util;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.opendaylight.aaa.AuthenticationBuilder;
import org.opendaylight.aaa.api.Authentication;
import org.opendaylight.aaa.api.Claim;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.TokenCacheTimes;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.Tokencache;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.TokenList;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.TokenListBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.TokenListKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.token_list.UserTokens;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.token_list.UserTokensBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.token_list.UserTokensKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.Claims;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.ClaimsBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.ClaimsKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class AuthNStoreUtil {

    public static InstanceIdentifier<Claims> createInstIdentifierForTokencache(String token) {
        if (token == null || token.length() == 0)
            return null;

        InstanceIdentifier<Claims> claims_iid = InstanceIdentifier.builder(Tokencache.class)
                                                                  .child(Claims.class,
                                                                          new ClaimsKey(token))
                                                                  .build();
        return claims_iid;
    }

    public static InstanceIdentifier<UserTokens> createInstIdentifierUserTokens(String userId,
            String token) {
        if (userId == null || userId.length() == 0 || token == null || token.length() == 0)
            return null;

        InstanceIdentifier<UserTokens> userTokens_iid = InstanceIdentifier.builder(
                TokenCacheTimes.class)
                                                                          .child(TokenList.class,
                                                                                  new TokenListKey(
                                                                                          userId))
                                                                          .child(UserTokens.class,
                                                                                  new UserTokensKey(
                                                                                          token))
                                                                          .build();
        return userTokens_iid;
    }

    public static Claims createClaimsRecord(String token, Authentication auth) {
        if (auth == null || token == null || token.length() == 0)
            return null;

        ClaimsKey claimsKey = new ClaimsKey(token);
        ClaimsBuilder claimsBuilder = new ClaimsBuilder();
        claimsBuilder.setClientId(auth.clientId());
        claimsBuilder.setDomain(auth.domain());
        claimsBuilder.setKey(claimsKey);
        List<String> roles = new ArrayList<String>();
        roles.addAll(auth.roles());
        claimsBuilder.setRoles(roles);
        claimsBuilder.setToken(token);
        claimsBuilder.setUser(auth.user());
        claimsBuilder.setUserId(auth.userId());
        return claimsBuilder.build();
    }

    public static UserTokens createUserTokens(String token, Long expiration) {
        if (expiration == null || token == null || token.length() == 0)
            return null;

        UserTokensBuilder userTokensBuilder = new UserTokensBuilder();
        userTokensBuilder.setTokenid(token);
        BigInteger timestamp = BigInteger.valueOf(System.currentTimeMillis());
        userTokensBuilder.setTimestamp(timestamp);
        userTokensBuilder.setExpiration(expiration);
        userTokensBuilder.setKey(new UserTokensKey(token));
        return userTokensBuilder.build();
    }

    public static TokenList createTokenList(UserTokens tokens, String userId) {
        if (tokens == null || userId == null || userId.length() == 0)
            return null;

        TokenListBuilder tokenListBuilder = new TokenListBuilder();
        tokenListBuilder.setUserId(userId);
        tokenListBuilder.setKey(new TokenListKey(userId));
        List<UserTokens> userTokens = new ArrayList<UserTokens>();
        userTokens.add(tokens);
        tokenListBuilder.setUserTokens(userTokens);
        return tokenListBuilder.build();
    }

    public static Authentication convertClaimToAuthentication(final Claims claims, Long expiration) {
        if (claims == null)
            return null;

        Claim claim = new Claim() {
            @Override
            public String clientId() {
                return claims.getClientId();
            }

            @Override
            public String userId() {
                return claims.getUserId();
            }

            @Override
            public String user() {
                return claims.getUser();
            }

            @Override
            public String domain() {
                return claims.getDomain();
            }

            @Override
            public Set<String> roles() {
                return new HashSet<>(claims.getRoles());
            }
        };
        AuthenticationBuilder authBuilder = new AuthenticationBuilder(claim);
        authBuilder.setExpiration(expiration);
        return authBuilder.build();
    }
}