Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leonard84/3b5ca97d54b45d589a3f727dd1a6a5a6 to your computer and use it in GitHub Desktop.
Save leonard84/3b5ca97d54b45d589a3f727dd1a6a5a6 to your computer and use it in GitHub Desktop.
MITREid SpringBoot
mitre:
scopes:
additional:
custom_scope:
- email
- phone
another:
- first_name
- birthday
/*******************************************************************************
* Copyright 2017 The MITRE Corporation
* and the MIT Internet Trust Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import org.mitre.openid.connect.service.ScopeClaimTranslationService;
import org.springframework.stereotype.Service;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
/**
* Service to map scopes to claims, and claims to Java field names
*
* @author Amanda Anganes
*/
@Service("scopeClaimTranslator")
public class DefaultScopeClaimTranslationService implements ScopeClaimTranslationService {
private SetMultimap<String, String> scopesToClaims = HashMultimap.create();
/**
* Default constructor; initializes scopesToClaims map
*/
@Inject
public DefaultScopeClaimTranslationService(ScopeToClaimProperties properties) {
if (properties.isIncludeDefault()) {
scopesToClaims.put("openid", "sub");
scopesToClaims.put("profile", "name");
scopesToClaims.put("profile", "preferred_username");
scopesToClaims.put("profile", "given_name");
scopesToClaims.put("profile", "family_name");
scopesToClaims.put("profile", "middle_name");
scopesToClaims.put("profile", "nickname");
scopesToClaims.put("profile", "profile");
scopesToClaims.put("profile", "picture");
scopesToClaims.put("profile", "website");
scopesToClaims.put("profile", "gender");
scopesToClaims.put("profile", "zoneinfo");
scopesToClaims.put("profile", "locale");
scopesToClaims.put("profile", "updated_at");
scopesToClaims.put("profile", "birthdate");
scopesToClaims.put("email", "email");
scopesToClaims.put("email", "email_verified");
scopesToClaims.put("phone", "phone_number");
scopesToClaims.put("phone", "phone_number_verified");
scopesToClaims.put("address", "address");
}
properties.getAdditional().entrySet().forEach(entry -> scopesToClaims.putAll(entry.getKey(), entry.getValue()));
}
/* (non-Javadoc)
* @see org.mitre.openid.connect.service.ScopeClaimTranslationService#getClaimsForScope(java.lang.String)
*/
@Override
public Set<String> getClaimsForScope(String scope) {
if (scopesToClaims.containsKey(scope)) {
return scopesToClaims.get(scope);
} else {
return new HashSet<>();
}
}
/* (non-Javadoc)
* @see org.mitre.openid.connect.service.ScopeClaimTranslationService#getClaimsForScopeSet(java.util.Set)
*/
@Override
public Set<String> getClaimsForScopeSet(Set<String> scopes) {
Set<String> result = new HashSet<>();
for (String scope : scopes) {
result.addAll(getClaimsForScope(scope));
}
return result;
}
}
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mitre.scopes")
public class ScopeToClaimProperties {
private boolean includeDefault = true;
private Map<String, List<String>> additional = new HashMap<>();
public boolean isIncludeDefault() {
return includeDefault;
}
public void setIncludeDefault(boolean includeDefault) {
this.includeDefault = includeDefault;
}
public Map<String, List<String>> getAdditional() {
return additional;
}
public void setAdditional(Map<String, List<String>> additional) {
this.additional = additional;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment