rack-auth-gitlabapi 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +79 -0
- data/Rakefile +36 -0
- data/lib/rack/auth/gitlabapi.rb +75 -0
- data/lib/rack/auth/gitlabapi/version.rb +5 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ebe645ba2b6fac7e9b44a65824c25797cf4bb844e1424b4e8c9a9f77bb643300
|
4
|
+
data.tar.gz: 564c1bfaff0384ee4d8fedd63eb0455fa49f5e7e9f1ad219b20b59f9db18d218
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 80f474b9928d129a4b92a66ef11308215ed4e57f153edb5bc59b0a263dac2197ed9407c87b8278b74f3ee25351ae1e286a27367cb3cc109438da2cb901df1ced
|
7
|
+
data.tar.gz: f05f6f7d7a69a08032512416bed021301d47900a6600e365d42bfa1f267ad55a54aea7b6aac9eedd22bb78998e64c15ae0c3298b33f6b0b4530e165741e5c9e5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 iXmedia
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# rack-auth-gitlabapi
|
2
|
+
|
3
|
+
Rack Middleware for GitLab API authentication
|
4
|
+
|
5
|
+
## Presentation
|
6
|
+
|
7
|
+
Rack::Auth::Gitlabapi is a basic authentication module with GitLab API authentication support.
|
8
|
+
|
9
|
+
It's based on Rack:Auth::Basic from the Rack main Project.
|
10
|
+
|
11
|
+
This is an additional module for Rack to authenticate users against the GitLab API.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
In your Gemfile, add :
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem "rack-auth-gitlabapi"
|
19
|
+
```
|
20
|
+
|
21
|
+
Create a gitlab.yml at the same level as the config.ru file :
|
22
|
+
|
23
|
+
```yml
|
24
|
+
endpoint: https://example.net/api/v4
|
25
|
+
```
|
26
|
+
|
27
|
+
In you config.ru, simply add :
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'rubygems'
|
31
|
+
require 'bundler'
|
32
|
+
require 'rack'
|
33
|
+
|
34
|
+
Bundler.require
|
35
|
+
|
36
|
+
require File.dirname(__FILE__) + '/your_app.rb'
|
37
|
+
|
38
|
+
use Rack::Auth::Gitlabapi
|
39
|
+
run Sinatra::Application
|
40
|
+
```
|
41
|
+
|
42
|
+
This configuration activate the Basic Authentication for the entire application.
|
43
|
+
|
44
|
+
To use custom configuration file :
|
45
|
+
```ruby
|
46
|
+
use Rack::Auth::Gitlabapi, file: '/path/to/my/gitlab.yml'
|
47
|
+
```
|
48
|
+
|
49
|
+
## Advanced
|
50
|
+
|
51
|
+
To protect some routes according to the parameters of the Gitlab user :
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require 'rubygems'
|
55
|
+
require 'bundler'
|
56
|
+
require 'rack'
|
57
|
+
|
58
|
+
Bundler.require
|
59
|
+
|
60
|
+
require File.dirname(__FILE__) + '/myapp.rb'
|
61
|
+
|
62
|
+
class CustomGitlabapi < Rack::Auth::Gitlabapi
|
63
|
+
def call(env)
|
64
|
+
request = Rack::Request.new(env)
|
65
|
+
response = super(env)
|
66
|
+
|
67
|
+
return unauthorized if user.nil?
|
68
|
+
|
69
|
+
if request.path == '/upload' or request.post?
|
70
|
+
return unauthorized if (!user.can_create_project || user.external)
|
71
|
+
end
|
72
|
+
|
73
|
+
return response
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
use CustomGitlabapi
|
78
|
+
run Sinatra::Application
|
79
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'rack-auth-gitlabapi'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'test'
|
31
|
+
t.pattern = 'test/**/*_test.rb'
|
32
|
+
t.verbose = false
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
task default: :test
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'rack/auth/abstract/handler'
|
3
|
+
require 'rack/auth/abstract/request'
|
4
|
+
require 'gitlab'
|
5
|
+
require 'yaml'
|
6
|
+
|
7
|
+
module Rack
|
8
|
+
module Auth
|
9
|
+
|
10
|
+
class Config
|
11
|
+
def initialize(options = { file: './gitlab.yml'})
|
12
|
+
@values = ::YAML.load_file(::File.expand_path(options[:file], Dir.pwd))
|
13
|
+
@values.keys.each do |key|
|
14
|
+
@values[key.to_sym] = @values.delete(key)
|
15
|
+
end
|
16
|
+
@values.keys.each do |meth|
|
17
|
+
bloc = Proc.new {@values[meth] }
|
18
|
+
self.class.send :define_method, meth, &bloc
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class Gitlabapi < Basic
|
24
|
+
|
25
|
+
attr_reader :config
|
26
|
+
|
27
|
+
def initialize(app, config_options = {})
|
28
|
+
super(app)
|
29
|
+
@config = Config.new(config_options)
|
30
|
+
end
|
31
|
+
|
32
|
+
def call(env)
|
33
|
+
auth = Gitlabapi::Request.new(env)
|
34
|
+
return unauthorized unless auth.provided?
|
35
|
+
return bad_request unless auth.basic?
|
36
|
+
if valid?(auth)
|
37
|
+
env['REMOTE_USER'] = auth.username
|
38
|
+
return @app.call(env)
|
39
|
+
end
|
40
|
+
unauthorized
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def valid?(auth)
|
46
|
+
Gitlab.configure do |config|
|
47
|
+
config.endpoint = @config.endpoint
|
48
|
+
config.private_token = auth.password
|
49
|
+
end
|
50
|
+
|
51
|
+
begin
|
52
|
+
@user = Gitlab.user
|
53
|
+
if @user.username == auth.username || @user.email == auth.username
|
54
|
+
return true
|
55
|
+
else
|
56
|
+
return false
|
57
|
+
end
|
58
|
+
rescue
|
59
|
+
return false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def user
|
64
|
+
return @user
|
65
|
+
end
|
66
|
+
|
67
|
+
class Request < Basic::Request
|
68
|
+
def password
|
69
|
+
credentials.last
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-auth-gitlabapi
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- iXmedia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.0.5
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.0.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: gitlab
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.4.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.4.0
|
41
|
+
description: 'rack-auth-gitlabapi : provide GitLab API authentication for Rack middleware'
|
42
|
+
email: suivi@ixmedia.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- MIT-LICENSE
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/rack/auth/gitlabapi.rb
|
51
|
+
- lib/rack/auth/gitlabapi/version.rb
|
52
|
+
homepage: https://github.com/ixmedia/rack-auth-gitlabapi
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.7.6
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Rack middleware providing GitLab API authentication
|
76
|
+
test_files: []
|