summaryrefslogtreecommitdiff
path: root/lib/api/applications.rb
blob: 2afe8763d9d0fc104f433c596d45930538d70987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module API
  # External applications API
  class Applications < ::API::Base
    before { authenticated_as_admin! }

    resource :applications do
      helpers do
        def validate_redirect_uri(value)
          uri = ::URI.parse(value)
          !uri.is_a?(URI::HTTP) || uri.host
        rescue URI::InvalidURIError
          false
        end
      end

      desc 'Create a new application' do
        detail 'This feature was introduced in GitLab 10.5'
        success Entities::ApplicationWithSecret
      end
      params do
        requires :name, type: String, desc: 'Application name'
        requires :redirect_uri, type: String, desc: 'Application redirect URI'
        requires :scopes, type: String, desc: 'Application scopes'

        optional :confidential, type: Boolean, default: true,
          desc: 'Application will be used where the client secret is confidential'
      end
      post do
        # Validate that host in uri is specified
        # Please remove it when https://github.com/doorkeeper-gem/doorkeeper/pull/1440 is merged
        # and the doorkeeper gem version is bumped
        unless validate_redirect_uri(declared_params[:redirect_uri])
          render_api_error!({ redirect_uri: ["must be an absolute URI."] }, :bad_request)
        end

        application = Doorkeeper::Application.new(declared_params)

        if application.save
          present application, with: Entities::ApplicationWithSecret
        else
          render_validation_error! application
        end
      end

      desc 'Get applications' do
        success Entities::Application
      end
      get do
        applications = ApplicationsFinder.new.execute
        present applications, with: Entities::Application
      end

      desc 'Delete an application'
      delete ':id' do
        application = ApplicationsFinder.new(params).execute
        application.destroy

        no_content!
      end
    end
  end
end