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

com.formkiq.server.api.OAuthClientsController Maven / Gradle / Ivy

/*
 * Copyright (C) 2016 FormKiQ Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package com.formkiq.server.api;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.ClientListDTO;
import com.formkiq.server.service.OAuthService;
import com.formkiq.server.service.SpringSecurityService;

/**
 * Form Group Rest Services.
 *
 */
@RestController
public class OAuthClientsController extends AbstractRestController {

    /** Client Create URL. */
    public static final String API_CLIENT = "/api/clients";

    /** Client GET URL. */
    public static final String API_CLIENT_GET = API_CLIENT + "/get";

    /** Client Save URL. */
    public static final String API_CLIENT_SAVE = API_CLIENT + "/save";

    /** Client Create URL. */
    public static final String API_CLIENT_LIST = API_CLIENT + "/list";

    /** Client Delete URL. */
    public static final String API_CLIENT_DELETE = API_CLIENT + "/delete";

    /** OAuthService. */
    @Autowired
    private OAuthService oauthservice;

    /** SpringSecurityService. */
    @Autowired
    private SpringSecurityService securityService;

    /**
     * Gets a Client.
     * @param request {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @param client {@link String}
     * @throws IOException IOException
     * @return {@link ClientDTO}
     */
    @Transactional
    @RequestMapping(API_CLIENT_GET)
    public ClientDTO get(
            final HttpServletRequest request,
            final HttpServletResponse response,
            @RequestParam(value = "client", required = true)
            final String client) throws IOException {

        getApiVersion(request);
        this.securityService.verifyUserHasAccessToClient(client);

        UserDetails user = this.securityService.getUserDetails();
        return this.oauthservice.findClient(user, client);
    }

    /**
     * Creates Form Group.
     * @param request {@link HttpServletRequest}
     * @param name String
     * @param client String
     * @param secret String
     * @return ApiStringResponse
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_CLIENT_SAVE)
    public ApiMessageResponse save(
            final HttpServletRequest request,
            @RequestParam(value = "clientname", required = false)
            final String name,
            @RequestParam(value = "client", required = true)
            final String client,
            @RequestParam(value = "clientsecret", required = false)
            final String secret) {

        this.oauthservice.save(name, client, secret);
        return new ApiMessageResponse("Client saved");
    }

    /**
     * List Apps.
     * @param request {@link HttpServletRequest}
     * @param token {@link String}
     * @return ClientListDTO
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_CLIENT_LIST)
    public ClientListDTO list(final HttpServletRequest request,
            @RequestParam(value = "token", required = false)
            final String token) {

        return this.oauthservice.list(token);
    }

    /**
     * Delete Client.
     * @param request {@link HttpServletRequest}
     * @param client {@link String}
     * @return ApiStringResponse
     */
    @Transactional
    @Secured({ "ROLE_ADMIN" })
    @RequestMapping(API_CLIENT_DELETE)
    public ApiMessageResponse delete(final HttpServletRequest request,
            @RequestParam(value = "client", required = true)
            final String client) {

        this.oauthservice.deleteClient(client);
        return new ApiMessageResponse("Client deleted");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy