001/*
002 *  Copyright 2021 Anyware Services
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 */
016package org.ametys.plugins.extrausermgt.authentication.msal;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.Set;
021
022import org.apache.avalon.framework.component.Component;
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025
026import org.ametys.runtime.plugin.ExtensionPoint;
027
028/**
029 * This extension point handle a list of scopes for the Azure Active Directory.
030 */
031public class OIDCScopesExtensionPoint implements ExtensionPoint<String>, Component
032{
033    /** The avalon role */
034    public static final String ROLE = OIDCScopesExtensionPoint.class.getName();
035    
036    private Set<String> _extensionIds = new HashSet<>();
037    private Set<String> _scopes = new HashSet<>();
038    
039    public void addExtension(String id, String pluginName, String featureName, Configuration configuration) throws ConfigurationException
040    {
041        _extensionIds.add(id);
042        for (Configuration scopeConfiguration : configuration.getChildren("scope"))
043        {
044            _scopes.add(scopeConfiguration.getValue());
045        }
046    }
047
048    public void initializeExtensions() throws Exception
049    {
050        _scopes.add("openid");
051    }
052
053    public boolean hasExtension(String id)
054    {
055        return _extensionIds.contains(id);
056    }
057
058    public String getExtension(String id)
059    {
060        return hasExtension(id) ? id : null;
061    }
062
063    public Set<String> getExtensionsIds()
064    {
065        return Collections.unmodifiableSet(_extensionIds);
066    }
067
068    /**
069     * Get all scopes
070     * @return the scope set
071     */
072    public Set<String> getScopes()
073    {
074        return Collections.unmodifiableSet(_scopes);
075    }
076}