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

com.github.alexdlaird.ngrok.protocol.TunnelOAuth Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2021-2024 Alex Laird
 *
 * SPDX-License-Identifier: MIT
 */

package com.github.alexdlaird.ngrok.protocol;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * An object that represents the OAuth configuration for a {@link CreateTunnel}.
 */
public class TunnelOAuth {

    private final String provider;
    private final List scopes;
    private final List allowEmails;
    private final List allowDomains;

    private TunnelOAuth(final Builder builder) {
        this.provider = builder.provider;
        this.scopes = builder.scopes;
        this.allowDomains = builder.allowDomains;
        this.allowEmails = builder.allowEmails;
    }

    /**
     * Get the OAuth provider.
     */
    public String getProvider() {
        return provider;
    }

    /**
     * Get the list of OAuth scopes.
     */
    public List getScopes() {
        return scopes;
    }

    /**
     * Get the list of OAuth allowed emails.
     */
    public List getAllowEmails() {
        return allowEmails;
    }

    /**
     * Get the list of OAuth allowed domains.
     */
    public List getAllowDomains() {
        return allowDomains;
    }

    /**
     * Builder for OAuth configuration that conforms to ngrok's tunnel definition. See docs for that class for example usage.
     */
    public static class Builder {

        private String provider;
        private List scopes;
        private List allowEmails;
        private List allowDomains;

        /**
         * Construct TunnelOAuth Builder.
         */
        public Builder() {
        }

        /**
         * Construct a TunnelOAuth Builder from tunnel definition of oauth.
         *
         * @param tunnelOAuthDefinition The map of Tunnel OAuth attributes.
         */
        public Builder(final Map tunnelOAuthDefinition) {
            if (tunnelOAuthDefinition.containsKey("provider")) {
                this.provider = (String) tunnelOAuthDefinition.get("provider");
            }
            if (tunnelOAuthDefinition.containsKey("scopes")) {
                this.scopes = Collections.unmodifiableList(
                    (List) tunnelOAuthDefinition.get("scopes")
                );
            }
            if (tunnelOAuthDefinition.containsKey("allow_emails")) {
                this.allowEmails = Collections.unmodifiableList(
                    (List) tunnelOAuthDefinition.get("allow_emails")
                );
            }
            if (tunnelOAuthDefinition.containsKey("allow_domains")) {
                this.allowDomains = Collections.unmodifiableList(
                    (List) tunnelOAuthDefinition.get("allow_domains")
                );
            }
        }

        /**
         * The OAuth provider. This setting is required. For a list of valid providers, see
         * ngrok's docs.
         */
        public Builder withProvider(final String provider) {
            this.provider = provider;
            return this;
        }

        /**
         * The list of OAuth scopes.
         */
        public Builder withScopes(final List scopes) {
            this.scopes = Collections.unmodifiableList(scopes);
            return this;
        }

        /**
         * The list of allowed OAuth emails.
         */
        public Builder withAllowEmails(final List emails) {
            this.allowEmails = Collections.unmodifiableList(emails);
            return this;
        }

        /**
         * The list of allowed OAuth domains.
         */
        public Builder withAllowDomains(final List domains) {
            this.allowDomains = Collections.unmodifiableList(domains);
            return this;
        }

        /**
         * Build the {@link TunnelOAuth}.
         */
        public TunnelOAuth build() {
            return new TunnelOAuth(this);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy