PkceHelper.java 3.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020-2040, 北京符节科技有限公司 (support@fujieid.com & https://www.fujieid.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * 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.
 */
16 17
package com.fujieid.jap.oauth2.pkce;

智布道's avatar
智布道 已提交
18
import com.fujieid.jap.core.context.JapAuthentication;
19
import com.fujieid.jap.oauth2.OAuthConfig;
20
import com.fujieid.jap.oauth2.Oauth2Util;
21
import com.google.common.collect.Maps;
22 23

import java.util.Map;
24
import java.util.Optional;
25 26 27 28 29 30 31 32 33

/**
 * Proof Key for Code Exchange by OAuth Public Client
 *
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0.0
 * @see <a href="https://tools.ietf.org/html/rfc7636" target="_blank">Proof Key for Code Exchange by OAuth Public Clients</a>
 * @since 1.0.0
 */
34
public class PkceHelper {
35

智布道's avatar
智布道 已提交
36 37 38
    private PkceHelper() {
    }

39 40 41
    /**
     * Create the parameters required by PKCE
     *
42
     * @param oAuthConfig oauth config
43
     * @return Map
44 45 46 47 48
     * @see <a href="https://tools.ietf.org/html/rfc7636#section-1.1" target="_blank">1.1. Protocol Flow</a>
     * @see <a href="https://tools.ietf.org/html/rfc7636#section-4.1" target="_blank">4.1. Client Creates a Code Verifier</a>
     * @see <a href="https://tools.ietf.org/html/rfc7636#section-4.2" target="_blank">4.2. Client Creates the Code Challenge</a>
     * @see <a href="https://tools.ietf.org/html/rfc7636#section-4.3" target="_blank"> Client Sends the Code Challenge with the Authorization Request</a>
     */
49 50 51 52 53 54 55
    public static Map<String, Object> generatePkceParameters(OAuthConfig oAuthConfig) {
        /*
        After the pkce enhancement protocol is enabled, the generation method of challenge code derived from
        the code verifier sent in the authorization request is `s256` by default
         */
        PkceCodeChallengeMethod pkceCodeChallengeMethod = Optional.ofNullable(oAuthConfig.getCodeChallengeMethod())
            .orElse(PkceCodeChallengeMethod.S256);
56

57 58 59 60 61 62
        Map<String, Object> params = Maps.newHashMap();
        String codeVerifier = Oauth2Util.generateCodeVerifier();
        String codeChallenge = Oauth2Util.generateCodeChallenge(pkceCodeChallengeMethod, codeVerifier);
        params.put(PkceParams.CODE_CHALLENGE, codeChallenge);
        params.put(PkceParams.CODE_CHALLENGE_METHOD, pkceCodeChallengeMethod);
        // The default cache is local map.
智布道's avatar
智布道 已提交
63
        JapAuthentication.getContext().getCache().set(oAuthConfig.getClientId(), codeVerifier, oAuthConfig.getCodeVerifierTimeout());
64
        return params;
65 66 67 68 69
    }

    /**
     * Gets the {@code code_verifier} in the cache
     *
70
     * @param clientId oauth clientId
71 72
     * @return {@code code_verifier}
     */
73
    public static String getCacheCodeVerifier(String clientId) {
智布道's avatar
智布道 已提交
74
        return (String) JapAuthentication.getContext().getCache().get(clientId);
75 76
    }
}