001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *******************************************************************************/
016
017package org.mitre.oauth2.model;
018
019import java.util.Collection;
020import java.util.HashSet;
021
022import javax.persistence.Basic;
023import javax.persistence.CollectionTable;
024import javax.persistence.Column;
025import javax.persistence.Convert;
026import javax.persistence.ElementCollection;
027import javax.persistence.Entity;
028import javax.persistence.FetchType;
029import javax.persistence.GeneratedValue;
030import javax.persistence.GenerationType;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.Table;
034import javax.persistence.Transient;
035
036import org.mitre.oauth2.model.convert.SimpleGrantedAuthorityStringConverter;
037import org.springframework.security.core.Authentication;
038import org.springframework.security.core.GrantedAuthority;
039
040/**
041 * This class stands in for an original Authentication object.
042 *
043 * @author jricher
044 *
045 */
046@Entity
047@Table(name="saved_user_auth")
048public class SavedUserAuthentication implements Authentication {
049
050        private static final long serialVersionUID = -1804249963940323488L;
051
052        private Long id;
053
054        private String name;
055
056        private Collection<GrantedAuthority> authorities;
057
058        private boolean authenticated;
059
060        private String sourceClass;
061
062        /**
063         * Create a Saved Auth from an existing Auth token
064         */
065        public SavedUserAuthentication(Authentication src) {
066                setName(src.getName());
067                setAuthorities(new HashSet<>(src.getAuthorities()));
068                setAuthenticated(src.isAuthenticated());
069
070                if (src instanceof SavedUserAuthentication) {
071                        // if we're copying in a saved auth, carry over the original class name
072                        setSourceClass(((SavedUserAuthentication) src).getSourceClass());
073                } else {
074                        setSourceClass(src.getClass().getName());
075                }
076        }
077
078        /**
079         * Create an empty saved auth
080         */
081        public SavedUserAuthentication() {
082
083        }
084
085        /**
086         * @return the id
087         */
088        @Id
089        @GeneratedValue(strategy = GenerationType.IDENTITY)
090        @Column(name = "id")
091        public Long getId() {
092                return id;
093        }
094
095        /**
096         * @param id the id to set
097         */
098        public void setId(Long id) {
099                this.id = id;
100        }
101
102        @Override
103        @Basic
104        @Column(name="name")
105        public String getName() {
106                return name;
107        }
108
109        @Override
110        @ElementCollection(fetch = FetchType.EAGER)
111        @CollectionTable(
112                        name="saved_user_auth_authority",
113                        joinColumns=@JoinColumn(name="owner_id")
114                        )
115        @Convert(converter = SimpleGrantedAuthorityStringConverter.class)
116        @Column(name="authority")
117        public Collection<GrantedAuthority> getAuthorities() {
118                return authorities;
119        }
120
121        @Override
122        @Transient
123        public Object getCredentials() {
124                return "";
125        }
126
127        @Override
128        @Transient
129        public Object getDetails() {
130                return null;
131        }
132
133        @Override
134        @Transient
135        public Object getPrincipal() {
136                return getName();
137        }
138
139        @Override
140        @Basic
141        @Column(name="authenticated")
142        public boolean isAuthenticated() {
143                return authenticated;
144        }
145
146        @Override
147        public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
148                this.authenticated = isAuthenticated;
149        }
150
151        /**
152         * @return the sourceClass
153         */
154        @Basic
155        @Column(name="source_class")
156        public String getSourceClass() {
157                return sourceClass;
158        }
159
160        /**
161         * @param sourceClass the sourceClass to set
162         */
163        public void setSourceClass(String sourceClass) {
164                this.sourceClass = sourceClass;
165        }
166
167        /**
168         * @param name the name to set
169         */
170        public void setName(String name) {
171                this.name = name;
172        }
173
174        /**
175         * @param authorities the authorities to set
176         */
177        public void setAuthorities(Collection<GrantedAuthority> authorities) {
178                this.authorities = authorities;
179        }
180
181
182}