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/**
017 *
018 */
019package org.mitre.openid.connect.client;
020
021import org.springframework.security.core.GrantedAuthority;
022
023import com.google.common.base.Strings;
024
025/**
026 *
027 * Simple authority representing a user at an issuer.
028 *
029 * @author jricher
030 *
031 */
032public class SubjectIssuerGrantedAuthority implements GrantedAuthority {
033
034        private static final long serialVersionUID = 5584978219226664794L;
035
036        private final String subject;
037        private final String issuer;
038
039        /**
040         * @param subject
041         * @param issuer
042         */
043        public SubjectIssuerGrantedAuthority(String subject, String issuer) {
044                if (Strings.isNullOrEmpty(subject) || Strings.isNullOrEmpty(issuer)) {
045                        throw new IllegalArgumentException("Neither subject nor issuer may be null or empty");
046                }
047                this.subject = subject;
048                this.issuer = issuer;
049        }
050
051        /**
052         * Returns a string formed by concatenating the subject with the issuer, separated by _ and prepended with OIDC_
053         *
054         * For example, the user "bob" from issuer "http://id.example.com/" would return the authority string of:
055         *
056         * OIDC_bob_http://id.example.com/
057         */
058        @Override
059        public String getAuthority() {
060                return "OIDC_" + subject + "_" + issuer;
061        }
062
063        /**
064         * @return the subject
065         */
066        public String getSubject() {
067                return subject;
068        }
069
070        /**
071         * @return the issuer
072         */
073        public String getIssuer() {
074                return issuer;
075        }
076
077        /* (non-Javadoc)
078         * @see java.lang.Object#hashCode()
079         */
080        @Override
081        public int hashCode() {
082                final int prime = 31;
083                int result = 1;
084                result = prime * result + ((issuer == null) ? 0 : issuer.hashCode());
085                result = prime * result + ((subject == null) ? 0 : subject.hashCode());
086                return result;
087        }
088
089        /* (non-Javadoc)
090         * @see java.lang.Object#equals(java.lang.Object)
091         */
092        @Override
093        public boolean equals(Object obj) {
094                if (this == obj) {
095                        return true;
096                }
097                if (obj == null) {
098                        return false;
099                }
100                if (!(obj instanceof SubjectIssuerGrantedAuthority)) {
101                        return false;
102                }
103                SubjectIssuerGrantedAuthority other = (SubjectIssuerGrantedAuthority) obj;
104                if (issuer == null) {
105                        if (other.issuer != null) {
106                                return false;
107                        }
108                } else if (!issuer.equals(other.issuer)) {
109                        return false;
110                }
111                if (subject == null) {
112                        if (other.subject != null) {
113                                return false;
114                        }
115                } else if (!subject.equals(other.subject)) {
116                        return false;
117                }
118                return true;
119        }
120
121        @Override
122        public String toString() {
123                return getAuthority();
124        }
125}