001/*******************************************************************************
002 * Copyright 2014 The MITRE Corporation
003 *   and the MIT Kerberos and Internet Trust Consortium
004 * 
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 *   http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 ******************************************************************************/
017package org.mitre.openid.connect.model;
018
019import java.util.ArrayList;
020import java.util.Collection;
021
022import org.mitre.openid.connect.config.ServerConfiguration;
023import org.springframework.security.authentication.AbstractAuthenticationToken;
024import org.springframework.security.core.GrantedAuthority;
025
026import com.google.common.collect.ImmutableMap;
027
028/**
029 * 
030 * @author Michael Walsh, Justin Richer
031 * 
032 */
033public class OIDCAuthenticationToken extends AbstractAuthenticationToken {
034
035        private static final long serialVersionUID = 22100073066377804L;
036
037        private final ImmutableMap<String, String> principal;
038        private final String idTokenValue; // string representation of the id token
039        private final String accessTokenValue; // string representation of the access token
040        private final String refreshTokenValue; // string representation of the refresh token
041        private final String issuer; // issuer URL (parsed from the id token)
042        private final String sub; // user id (parsed from the id token)
043
044        private final transient ServerConfiguration serverConfiguration; // server configuration used to fulfill this token, don't serialize it
045        private final UserInfo userInfo; // user info container, don't serialize it b/c it might be huge and can be re-fetched
046
047        /**
048         * Constructs OIDCAuthenticationToken with a full set of authorities, marking this as authenticated.
049         * 
050         * Set to authenticated.
051         * 
052         * Constructs a Principal out of the subject and issuer.
053         * @param subject
054         * @param authorities
055         * @param principal
056         * @param idToken
057         */
058        public OIDCAuthenticationToken(String subject, String issuer,
059                        UserInfo userInfo, Collection<? extends GrantedAuthority> authorities,
060                        String idTokenValue, String accessTokenValue, String refreshTokenValue) {
061
062                super(authorities);
063
064                this.principal = ImmutableMap.of("sub", subject, "iss", issuer);
065                this.userInfo = userInfo;
066                this.sub = subject;
067                this.issuer = issuer;
068                this.idTokenValue = idTokenValue;
069                this.accessTokenValue = accessTokenValue;
070                this.refreshTokenValue = refreshTokenValue;
071
072                this.serverConfiguration = null; // we don't need a server config anymore
073
074                setAuthenticated(true);
075        }
076
077        /**
078         * Constructs OIDCAuthenticationToken for use as a data shuttle from the filter to the auth provider.
079         * 
080         * Set to not-authenticated.
081         * 
082         * Constructs a Principal out of the subject and issuer.
083         * @param sub
084         * @param idToken
085         */
086        public OIDCAuthenticationToken(String subject, String issuer,
087                        ServerConfiguration serverConfiguration,
088                        String idTokenValue, String accessTokenValue, String refreshTokenValue) {
089
090                super(new ArrayList<GrantedAuthority>(0));
091
092                this.principal = ImmutableMap.of("sub", subject, "iss", issuer);
093                this.sub = subject;
094                this.issuer = issuer;
095                this.idTokenValue = idTokenValue;
096                this.accessTokenValue = accessTokenValue;
097                this.refreshTokenValue = refreshTokenValue;
098
099                this.userInfo = null; // we don't have a UserInfo yet
100
101                this.serverConfiguration = serverConfiguration;
102
103
104                setAuthenticated(false);
105        }
106
107        /*
108         * (non-Javadoc)
109         * 
110         * @see org.springframework.security.core.Authentication#getCredentials()
111         */
112        @Override
113        public Object getCredentials() {
114                return accessTokenValue;
115        }
116
117        /**
118         * Get the principal of this object, an immutable map of the subject and issuer.
119         */
120        @Override
121        public Object getPrincipal() {
122                return principal;
123        }
124
125        public String getSub() {
126                return sub;
127        }
128
129        /**
130         * @return the idTokenValue
131         */
132        public String getIdTokenValue() {
133                return idTokenValue;
134        }
135
136        /**
137         * @return the accessTokenValue
138         */
139        public String getAccessTokenValue() {
140                return accessTokenValue;
141        }
142
143        /**
144         * @return the refreshTokenValue
145         */
146        public String getRefreshTokenValue() {
147                return refreshTokenValue;
148        }
149
150        /**
151         * @return the serverConfiguration
152         */
153        public ServerConfiguration getServerConfiguration() {
154                return serverConfiguration;
155        }
156
157        /**
158         * @return the issuer
159         */
160        public String getIssuer() {
161                return issuer;
162        }
163
164        /**
165         * @return the userInfo
166         */
167        public UserInfo getUserInfo() {
168                return userInfo;
169        }
170
171
172}