001/*******************************************************************************
002 * Copyright 2017 The MIT Internet Trust Consortium
003 *
004 * Portions copyright 2011-2013 The MITRE Corporation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *******************************************************************************/
018/**
019 *
020 */
021package org.mitre.oauth2.service.impl;
022
023import java.util.LinkedHashSet;
024import java.util.Set;
025
026import org.mitre.oauth2.model.SystemScope;
027import org.mitre.oauth2.repository.SystemScopeRepository;
028import org.mitre.oauth2.service.SystemScopeService;
029import org.springframework.beans.factory.annotation.Autowired;
030import org.springframework.stereotype.Service;
031
032import com.google.common.base.Function;
033import com.google.common.base.Predicate;
034import com.google.common.base.Predicates;
035import com.google.common.base.Strings;
036import com.google.common.collect.Collections2;
037import com.google.common.collect.Sets;
038
039/**
040 * @author jricher
041 *
042 */
043@Service("defaultSystemScopeService")
044public class DefaultSystemScopeService implements SystemScopeService {
045
046        @Autowired
047        private SystemScopeRepository repository;
048
049        private Predicate<SystemScope> isDefault = new Predicate<SystemScope>() {
050                @Override
051                public boolean apply(SystemScope input) {
052                        return (input != null && input.isDefaultScope());
053                }
054        };
055
056        private Predicate<SystemScope> isRestricted = new Predicate<SystemScope>() {
057                @Override
058                public boolean apply(SystemScope input) {
059                        return (input != null && input.isRestricted());
060                }
061        };
062
063        private Predicate<SystemScope> isReserved = new Predicate<SystemScope>() {
064                @Override
065                public boolean apply(SystemScope input) {
066                        return (input != null && getReserved().contains(input));
067                }
068        };
069
070        private Function<String, SystemScope> stringToSystemScope = new Function<String, SystemScope>() {
071                @Override
072                public SystemScope apply(String input) {
073                        if (Strings.isNullOrEmpty(input)) {
074                                return null;
075                        } else {
076                                // get the real scope if it's available
077                                SystemScope s = getByValue(input);
078                                if (s == null) {
079                                        // make a fake one otherwise
080                                        s = new SystemScope(input);
081                                }
082
083                                return s;
084                        }
085                }
086        };
087
088        private Function<SystemScope, String> systemScopeToString = new Function<SystemScope, String>() {
089                @Override
090                public String apply(SystemScope input) {
091                        if (input == null) {
092                                return null;
093                        } else {
094                                return input.getValue();
095                        }
096                }
097        };
098
099        /* (non-Javadoc)
100         * @see org.mitre.oauth2.service.SystemScopeService#getAll()
101         */
102        @Override
103        public Set<SystemScope> getAll() {
104                return repository.getAll();
105        }
106
107        /* (non-Javadoc)
108         * @see org.mitre.oauth2.service.SystemScopeService#getById(java.lang.Long)
109         */
110        @Override
111        public SystemScope getById(Long id) {
112                return repository.getById(id);
113        }
114
115        /* (non-Javadoc)
116         * @see org.mitre.oauth2.service.SystemScopeService#getByValue(java.lang.String)
117         */
118        @Override
119        public SystemScope getByValue(String value) {
120                return repository.getByValue(value);
121        }
122
123        /* (non-Javadoc)
124         * @see org.mitre.oauth2.service.SystemScopeService#remove(org.mitre.oauth2.model.SystemScope)
125         */
126        @Override
127        public void remove(SystemScope scope) {
128                repository.remove(scope);
129
130        }
131
132        /* (non-Javadoc)
133         * @see org.mitre.oauth2.service.SystemScopeService#save(org.mitre.oauth2.model.SystemScope)
134         */
135        @Override
136        public SystemScope save(SystemScope scope) {
137                if (!isReserved.apply(scope)) { // don't allow saving of reserved scopes
138                        return repository.save(scope);
139                } else {
140                        return null;
141                }
142        }
143
144        /* (non-Javadoc)
145         * @see org.mitre.oauth2.service.SystemScopeService#fromStrings(java.util.Set)
146         */
147        @Override
148        public Set<SystemScope> fromStrings(Set<String> scope) {
149                if (scope == null) {
150                        return null;
151                } else {
152                        return new LinkedHashSet<>(Collections2.filter(Collections2.transform(scope, stringToSystemScope), Predicates.notNull()));
153                }
154        }
155
156        /* (non-Javadoc)
157         * @see org.mitre.oauth2.service.SystemScopeService#toStrings(java.util.Set)
158         */
159        @Override
160        public Set<String> toStrings(Set<SystemScope> scope) {
161                if (scope == null) {
162                        return null;
163                } else {
164                        return new LinkedHashSet<>(Collections2.filter(Collections2.transform(scope, systemScopeToString), Predicates.notNull()));
165                }
166        }
167
168        /* (non-Javadoc)
169         * @see org.mitre.oauth2.service.SystemScopeService#scopesMatch(java.util.Set, java.util.Set)
170         */
171        @Override
172        public boolean scopesMatch(Set<String> expected, Set<String> actual) {
173
174                Set<SystemScope> ex = fromStrings(expected);
175                Set<SystemScope> act = fromStrings(actual);
176
177                for (SystemScope actScope : act) {
178                        // first check to see if there's an exact match
179                        if (!ex.contains(actScope)) {
180                                return false;
181                        } else {
182                                // if we did find an exact match, we need to check the rest
183                        }
184                }
185
186                // if we got all the way down here, the setup passed
187                return true;
188
189        }
190
191        @Override
192        public Set<SystemScope> getDefaults() {
193                return Sets.filter(getAll(), isDefault);
194        }
195
196
197        @Override
198        public Set<SystemScope> getReserved() {
199                return reservedScopes;
200        }
201
202        @Override
203        public Set<SystemScope> getRestricted() {
204                return Sets.filter(getAll(), isRestricted);
205        }
206
207        @Override
208        public Set<SystemScope> getUnrestricted() {
209                return Sets.filter(getAll(), Predicates.not(isRestricted));
210        }
211
212        @Override
213        public Set<SystemScope> removeRestrictedAndReservedScopes(Set<SystemScope> scopes) {
214                return Sets.filter(scopes, Predicates.not(Predicates.or(isRestricted, isReserved)));
215        }
216
217        @Override
218        public Set<SystemScope> removeReservedScopes(Set<SystemScope> scopes) {
219                return Sets.filter(scopes, Predicates.not(isReserved));
220        }
221
222}