NamedAdminAuthoritiesMapper.java

  1. /*******************************************************************************
  2.  * Copyright 2017 The MIT Internet Trust Consortium
  3.  *
  4.  * Portions copyright 2011-2013 The MITRE Corporation
  5.  *
  6.  * Licensed under the Apache License, Version 2.0 (the "License");
  7.  * you may not use this file except in compliance with the License.
  8.  * You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  *******************************************************************************/
  18. /**
  19.  *
  20.  */
  21. package org.mitre.openid.connect.client;

  22. import java.text.ParseException;
  23. import java.util.Collection;
  24. import java.util.HashSet;
  25. import java.util.Set;

  26. import org.mitre.openid.connect.model.UserInfo;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.security.core.GrantedAuthority;
  30. import org.springframework.security.core.authority.SimpleGrantedAuthority;

  31. import com.nimbusds.jwt.JWT;
  32. import com.nimbusds.jwt.JWTClaimsSet;

  33. /**
  34.  *
  35.  * Simple mapper that adds ROLE_USER to the authorities map for all queries,
  36.  * plus adds ROLE_ADMIN if the subject and issuer pair are found in the
  37.  * configurable "admins" set.
  38.  *
  39.  * @author jricher
  40.  *
  41.  */
  42. public class NamedAdminAuthoritiesMapper implements OIDCAuthoritiesMapper {

  43.     private static Logger logger = LoggerFactory.getLogger(NamedAdminAuthoritiesMapper.class);

  44.     private static final SimpleGrantedAuthority ROLE_ADMIN = new SimpleGrantedAuthority("ROLE_ADMIN");
  45.     private static final SimpleGrantedAuthority ROLE_USER = new SimpleGrantedAuthority("ROLE_USER");

  46.     private Set<SubjectIssuerGrantedAuthority> admins = new HashSet<>();

  47.     @Override
  48.     public Collection<? extends GrantedAuthority> mapAuthorities(JWT idToken, UserInfo userInfo) {

  49.         Set<GrantedAuthority> out = new HashSet<>();
  50.         try {
  51.             JWTClaimsSet claims = idToken.getJWTClaimsSet();

  52.             SubjectIssuerGrantedAuthority authority = new SubjectIssuerGrantedAuthority(claims.getSubject(), claims.getIssuer());
  53.             out.add(authority);

  54.             if (admins.contains(authority)) {
  55.                 out.add(ROLE_ADMIN);
  56.             }

  57.             // everybody's a user by default
  58.             out.add(ROLE_USER);

  59.         } catch (ParseException e) {
  60.             logger.error("Unable to parse ID Token inside of authorities mapper (huh?)");
  61.         }
  62.         return out;
  63.     }

  64.     /**
  65.      * @return the admins
  66.      */
  67.     public Set<SubjectIssuerGrantedAuthority> getAdmins() {
  68.         return admins;
  69.     }

  70.     /**
  71.      * @param admins the admins to set
  72.      */
  73.     public void setAdmins(Set<SubjectIssuerGrantedAuthority> admins) {
  74.         this.admins = admins;
  75.     }

  76. }