All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dingtalk.baymax.framework.sdk.mercury.plugin.ToolAuthentication Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.dingtalk.baymax.framework.sdk.mercury.plugin;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;

import java.util.Objects;

/**
 * @author xuechen
 */
public class ToolAuthentication {
    public static final ToolAuthentication DEFAULT = ToolAuthentication.of();

    private static final String HEADER_ACCESS_TOKEN_NAME = "x-acs-dingtalk-access-token";
    private static final String MANIFEST_AUTH_KEY = "auth";
    private static final String MANIFEST_AUTH_TYPE_KEY = "type";
    private static final String MANIFEST_AUTH_VERIFICATION_PARAM_KEY = "verification_param";
    private static final String MANIFEST_AUTH_VERIFICATION_PARAM_LOCATION_KEY = "in";
    private static final String MANIFEST_AUTH_VERIFICATION_PARAM_NAME_KEY = "name";

    private AuthType type;

    private VerificationParam verificationParam;

    public static class VerificationParam {
        private Location location;
        private String name;

        public Location getLocation() {
            return location;
        }

        public void setLocation(Location location) {
            this.location = location;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public enum AuthType {
        APPLICATION("application"),
        DELEGATED("delegated");

        private String type;

        AuthType(String type) {
            this.type = type;
        }

        public String getType() {
            return type;
        }

        public static AuthType parse(String type) {
            for (AuthType t : AuthType.values()) {
                if (Objects.equals(t.getType(), type)) {
                    return t;
                }
            }
            return APPLICATION;
        }
    }

    public enum Location {
        QUERY("query"),
        HEADER("header");

        private String location;

        Location(String location) {
            this.location = location;
        }

        public String getLocation() {
            return location;
        }

        public static Location parse(String location) {
            for (Location t : Location.values()) {
                if (Objects.equals(t.getLocation(), location)) {
                    return t;
                }
            }
            return HEADER;
        }
    }

    private static ToolAuthentication of() {
        ToolAuthentication toolAuthentication = new ToolAuthentication();
        toolAuthentication.setType(AuthType.APPLICATION);
        VerificationParam verificationParam = new VerificationParam();
        verificationParam.setLocation(Location.HEADER);
        verificationParam.setName(HEADER_ACCESS_TOKEN_NAME);
        toolAuthentication.setVerificationParam(verificationParam);
        return toolAuthentication;
    }

    public static ToolAuthentication of(JSONObject abilityForRuntime) {
        if (abilityForRuntime == null) {
            return null;
        }

        JSONObject authObj = abilityForRuntime.getJSONObject(MANIFEST_AUTH_KEY);
        if (authObj == null) {
            return null;
        }

        ToolAuthentication toolAuthentication = new ToolAuthentication();
        String typeString = authObj.getString(MANIFEST_AUTH_TYPE_KEY);
        JSONObject verificationParamObj = authObj.getJSONObject(MANIFEST_AUTH_VERIFICATION_PARAM_KEY);
        if (Strings.isNullOrEmpty(typeString) || verificationParamObj == null) {
            return null;
        }


        toolAuthentication.setType(AuthType.parse(authObj.getString(MANIFEST_AUTH_TYPE_KEY)));
        String in = verificationParamObj.getString(MANIFEST_AUTH_VERIFICATION_PARAM_LOCATION_KEY);
        String name = verificationParamObj.getString(MANIFEST_AUTH_VERIFICATION_PARAM_NAME_KEY);
        if (Strings.isNullOrEmpty(in) || Strings.isNullOrEmpty(name)) {
            return DEFAULT;
        }

        VerificationParam verificationParam = new VerificationParam();
        verificationParam.setLocation(Location.parse(in));
        verificationParam.setName(name);
        toolAuthentication.setVerificationParam(verificationParam);
        return toolAuthentication;
    }

    public AuthType getType() {
        return type;
    }

    public void setType(AuthType type) {
        this.type = type;
    }

    public VerificationParam getVerificationParam() {
        return verificationParam;
    }

    public void setVerificationParam(VerificationParam verificationParam) {
        this.verificationParam = verificationParam;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy