001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Salem Aouana
018 */
019
020package org.nuxeo.ecm.platform.oauth2.tokens;
021
022import java.util.List;
023
024import org.nuxeo.ecm.core.api.NuxeoException;
025import org.nuxeo.ecm.core.api.NuxeoPrincipal;
026import org.nuxeo.ecm.platform.oauth2.enums.NuxeoOAuth2TokenType;
027
028/**
029 * Manages oAuth2 tokens. A token can be:
030 * <ul>
031 * <li>Provided by Nuxeo, it's the oAuth2 server provider.</li>
032 * <li>Consumed by Nuxeo, it's a client of another server provider.</li>
033 * </ul>
034 *
035 * @since 11.1
036 */
037public interface OAuth2TokenService {
038
039    /**
040     * Gets the oAuth2 tokens as the given principal.
041     *
042     * @return the oAuth2 tokens
043     * @throws NuxeoException with 403 status code if the given principal doesn't have access to the oAuth2 tokens
044     */
045    List<NuxeoOAuth2Token> getTokens(NuxeoPrincipal principal);
046
047    /**
048     * Gets the OAuth2 tokens for the given user.
049     *
050     * @param nxuser the nuxeo user
051     * @return the oAuth2 tokens that match the given user
052     * @throws NullPointerException if {@code nxuser} is {@code null}
053     */
054    List<NuxeoOAuth2Token> getTokens(String nxuser);
055
056    /**
057     * Gets the oAuth2 tokens for a given type as the given principal.
058     **
059     * @param type the token type {@link NuxeoOAuth2TokenType}
060     * @return the oAuth2 tokens that match the type
061     * @throws NullPointerException if {@code type} is {@code null}
062     * @throws NuxeoException with 403 status code if the given principal doesn't have access to the oAuth2 tokens
063     */
064    List<NuxeoOAuth2Token> getTokens(NuxeoOAuth2TokenType type, NuxeoPrincipal principal);
065
066    /**
067     * Gets the OAuth2 tokens from a given user name and a type.
068     *
069     * @param nxuser the nuxeo user
070     * @param type the token type {@link NuxeoOAuth2TokenType}
071     * @return the oAuth2 tokens
072     * @throws NullPointerException if {@code nxuser} or {@code type} is {@code null}
073     */
074    List<NuxeoOAuth2Token> getTokens(String nxuser, NuxeoOAuth2TokenType type);
075
076    /**
077     * Finds the oAuth2 tokens that match the given {@code query} as the given principal. To be retrieved a token should
078     * match the {@code query} anywhere in the string value of the fields {@link NuxeoOAuth2Token#KEY_NUXEO_LOGIN},
079     * {@link NuxeoOAuth2Token#KEY_SERVICE_NAME}
080     *
081     * @param query the query to match
082     * @return the oAuth2 tokens that match the {@code query}
083     * @throws NuxeoException with 403 status code if the given principal doesn't have access to the oAuth2 tokens
084     */
085    List<NuxeoOAuth2Token> search(String query, NuxeoPrincipal principal);
086
087}