001/*
002 *  Copyright 2024 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.net.MalformedURLException;
019import java.net.URI;
020import java.net.URISyntaxException;
021import java.net.URL;
022import java.util.Map;
023
024/**
025 * Sign in through ADFS, using the OpenId Connect protocol.
026 */
027public class ADFSCredentialProvider extends AbstractMSALCredentialProvider
028{
029    private String _adfsServer;
030    
031    @Override
032    public void init(String id, String cpModelId, Map<String, Object> paramValues, String label) throws Exception
033    {
034        super.init(id, cpModelId, paramValues, label);
035        
036        _adfsServer = (String) paramValues.get("authentication.adfs.url");
037
038        String clientID = (String) paramValues.get("authentication.adfs.appid");
039        String clientSecret = (String) paramValues.get("authentication.adfs.clientsecret");
040        boolean silent = (boolean) paramValues.get("authentication.adfs.silent");
041
042        init(clientID, clientSecret, false, silent);
043    }
044    
045    @Override
046    protected String getAuthority()
047    {
048        return _adfsServer;
049    }
050    
051    public String getIssuer()
052    {
053        return getAuthority();
054    }
055    
056    public URL getJwkSetURL()
057    {
058        try
059        {
060            return new URI(getAuthority() + "/discovery/keys").toURL();
061        }
062        catch (MalformedURLException | URISyntaxException e)
063        {
064            throw new IllegalArgumentException("Invalid JWKSetURL", e);
065        }
066    }
067}