omniauth-mattermost 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e11c815a9d2a317e8845bf52c5706c37a84ee45c9ad869e7715e44be6324a23b
4
+ data.tar.gz: 9fcafdca2e202e9a727b439edd013e31be815762db9abc595189f19c507291ed
5
+ SHA512:
6
+ metadata.gz: e09a429fbfc62cabc1f595ea6a9a71a812769c0a43c35d31a44ee86224d632fbb6871a63c55ead56744034537e7b124191673430f90684a9334750f30b9cc473
7
+ data.tar.gz: ba2e251ae3501e9d0813e9306bbc812080cc767f4782a0509c15c26eb3ae117abe9e14cd2f5f7851be90ad8d33c5a955c8c5016613e6aae48b9f045f7b6f2c68
@@ -0,0 +1,22 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby-version: ["3.0", "3.1", "3.2", "3.3"]
11
+
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+ - name: Set up Ruby ${{ matrix.ruby-version }}
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby-version }}
18
+ - name: Build and test with Rake
19
+ run: |
20
+ gem install bundler
21
+ bundle install --jobs 4 --retry 3
22
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ /pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
19
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-mattermost.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ gem 'guard-bundler'
10
+ gem 'rb-fsevent'
11
+ gem 'growl'
12
+ gem 'rake'
13
+ gem 'graphlient'
14
+ end
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'rspec', :version => 2 do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { "spec" }
5
+ end
6
+
7
+ guard 'bundler' do
8
+ watch('Gemfile')
9
+ watch('omniauth-mattermost.gemspec')
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2021 Quentin Rousseau.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ ![Ruby](https://github.com/kwent/omniauth-mattermost/workflows/Ruby/badge.svg?branch=master)
2
+
3
+ # OmniAuth Mattermost
4
+
5
+ This is the official OmniAuth strategy for authenticating to Mattermost. To
6
+ use it, you'll need to sign up for an OAuth2 Client ID and Secret
7
+ on the [Mattermost Developer Page](https://community.servicenow.com/community?id=community_blog&sys_id=56086e4fdb9014146064eeb5ca961957).
8
+
9
+ ## Installation
10
+
11
+ ```ruby
12
+ gem 'omniauth-mattermost'
13
+ ```
14
+
15
+ ## Basic Usage
16
+
17
+ ```ruby
18
+ use OmniAuth::Builder do
19
+ provider :mattermost, ENV['MATTERMOST_CLIENT_ID'], ENV['MATTERMOST_CLIENT_SECRET'], { client_options: { site: 'https://<instance-id>.mattermost.com' } }
20
+ end
21
+ ```
22
+
23
+ ## Basic Usage Rails
24
+
25
+ In `config/initializers/mattermost.rb`
26
+
27
+ ```ruby
28
+ Rails.application.config.middleware.use OmniAuth::Builder do
29
+ provider :mattermost, ENV['MATTERMOST_CLIENT_ID'], ENV['MATTERMOST_CLIENT_SECRET'], { client_options: { site: 'https://<instance-id>.mattermost.com' } }
30
+ end
31
+ ```
32
+
33
+ ## Dynamic client_id and client_secret
34
+
35
+ In `config/initializers/mattermost.rb`
36
+
37
+ ```ruby
38
+ Rails.application.config.middleware.use OmniAuth::Builder do
39
+ setup_proc = lambda do |env|
40
+ req = Rack::Request.new(env)
41
+ env['omniauth.strategy'].options[:client_options][:site] = req.params['site']
42
+ env['omniauth.strategy'].options[:client_id] = req.params['client_id']
43
+ env['omniauth.strategy'].options[:client_secret] = req.params['client_secret']
44
+ end
45
+ provider :mattermost, nil, nil, { setup: setup_proc }
46
+ end
47
+ ```
48
+
49
+ More info: https://github.com/omniauth/omniauth/wiki/Setup-Phase
50
+
51
+ ## Semver
52
+
53
+ This project adheres to Semantic Versioning 2.0.0. Any violations of this scheme are considered to be bugs.
54
+ All changes will be tracked [here](https://github.com/kwent/omniauth-mattermost/releases).
55
+
56
+ ## License
57
+
58
+ Copyright (c) 2021 Quentin Rousseau.
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
61
+
62
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ desc 'Run specs'
8
+ task :default => :spec
@@ -0,0 +1,99 @@
1
+ require 'omniauth-oauth2'
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Mattermost < OmniAuth::Strategies::OAuth2
6
+
7
+ args %i[client_id client_secret]
8
+
9
+ option :name, 'mattermost'
10
+
11
+ option :client_options, {
12
+ site: "https://<instance-id>.mattermost.com",
13
+ authorize_url: "/oauth/authorize",
14
+ token_url: "/oauth/access_token",
15
+ response_type: 'code',
16
+ }
17
+
18
+ option :auth_token_params, {
19
+ grant_type: 'authorization_code',
20
+ }
21
+
22
+ # When `true`, client_id and client_secret are returned in extra['raw_info'].
23
+ option :extra_client_id_and_client_secret, false
24
+
25
+ def authorize_params
26
+ super.tap do |params|
27
+ %w[client_options].each do |v|
28
+ if request.params[v]
29
+ params[v.to_sym] = request.params[v]
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ def build_access_token
36
+ verifier = request.params["code"]
37
+ # Override regular client when using setup: proc
38
+ if env['omniauth.params']['client_id'] && env['omniauth.params']['client_secret'] && env['omniauth.params']['site']
39
+ client = ::OAuth2::Client.new(
40
+ env['omniauth.params']['client_id'],
41
+ env['omniauth.params']['client_secret'],
42
+ site: env['omniauth.params']['site'],
43
+ authorize_url: options.client_options.authorize_url,
44
+ token_url: options.client_options.token_url
45
+ )
46
+ client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)), deep_symbolize(options.auth_token_params))
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ uid { user['id'] }
53
+
54
+ info do
55
+ {
56
+ id: user['id'],
57
+ username: user['username'],
58
+ email: user['email'],
59
+ email_verified: user['email_verified']
60
+ }
61
+ end
62
+
63
+ extra do
64
+ { raw_info: raw_info, site: smart_site, instance_url: instance_url, user: user }
65
+ end
66
+
67
+ def raw_info
68
+ @raw_info ||= options[:extra_client_id_and_client_secret] ? { client_id: smart_client_id, client_secret: smart_client_secret } : {}
69
+ end
70
+
71
+ def smart_client_id
72
+ @smart_client_id ||= env['omniauth.params']['client_id'] || env['omniauth.strategy'].options.client_id
73
+ end
74
+
75
+ def smart_client_secret
76
+ @smart_client_secret ||= env['omniauth.params']['client_secret'] || env['omniauth.strategy'].options.client_secret
77
+ end
78
+
79
+ def smart_site
80
+ @site ||= env['omniauth.params']['site'] || env['omniauth.strategy'].options.site || options.client_options.site
81
+ end
82
+
83
+ def instance_url
84
+ @instance_url ||= smart_site if smart_site
85
+ end
86
+
87
+ def user
88
+ access_token.options[:mode] = :header
89
+ @user ||= access_token.get('api/v4/users/me', :headers => { 'Content-Type' => 'application/json' }).parsed
90
+ end
91
+
92
+ def callback_url
93
+ options.redirect_url || (full_host + callback_path)
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ OmniAuth.config.add_camelization 'mattermost', 'Mattermost'
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Mattermost
3
+ VERSION = "2.0.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "omniauth-mattermost/version"
2
+ require 'omniauth/strategies/mattermost'
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-mattermost/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Quentin Rousseau"]
6
+ gem.email = ["contact@quent.in"]
7
+ gem.description = %q{Official OmniAuth strategy for Mattermost.}
8
+ gem.summary = %q{Official OmniAuth strategy for Mattermost.}
9
+ gem.homepage = "https://github.com/kwent/omniauth-mattermost"
10
+ gem.license = "MIT"
11
+
12
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
13
+ gem.files = `git ls-files`.split("\n")
14
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ gem.name = "omniauth-mattermost"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = OmniAuth::Mattermost::VERSION
18
+
19
+ gem.required_ruby_version = ">= 3"
20
+
21
+ gem.add_dependency 'omniauth', '~> 2.0.0'
22
+ gem.add_dependency 'omniauth-oauth2', '>= 1.4.0', '< 2.0'
23
+ gem.add_development_dependency 'rspec', '~> 3.5'
24
+ gem.add_development_dependency 'rack-test'
25
+ gem.add_development_dependency 'simplecov'
26
+ gem.add_development_dependency 'webmock'
27
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe OmniAuth::Strategies::Mattermost do
4
+ let(:access_token) { instance_double('AccessToken', :options => {}, :[] => 'user') }
5
+ let(:parsed_response) { instance_double('ParsedResponse') }
6
+ let(:response) { instance_double('Response', :parsed => parsed_response) }
7
+
8
+ let(:enterprise_site) { 'https://some.other.site.com' }
9
+ let(:enterprise_authorize_url) { 'https://some.other.site.com/login/oauth/authorize' }
10
+ let(:enterprise_token_url) { 'https://some.other.site.com/login/oauth/token' }
11
+ let(:enterprise) do
12
+ OmniAuth::Strategies::Mattermost.new('MATTERMOST_CLIENT_ID', 'MATTERMOST_CLIENT_SECRET',
13
+ {
14
+ :client_options => {
15
+ :site => enterprise_site,
16
+ :authorize_url => enterprise_authorize_url,
17
+ :token_url => enterprise_token_url
18
+ }
19
+ }
20
+ )
21
+ end
22
+
23
+ subject do
24
+ OmniAuth::Strategies::Mattermost.new({})
25
+ end
26
+
27
+ before(:each) do
28
+ allow(subject).to receive(:access_token).and_return(access_token)
29
+ end
30
+
31
+ context 'client options' do
32
+ it 'should have correct site' do
33
+ expect(subject.options.client_options.site).to eq('https://<instance-id>.mattermost.com')
34
+ end
35
+
36
+ it 'should have correct authorize url' do
37
+ expect(subject.options.client_options.authorize_url).to eq('/oauth/authorize')
38
+ end
39
+
40
+ it 'should have correct token url' do
41
+ expect(subject.options.client_options.token_url).to eq('/oauth/access_token')
42
+ end
43
+
44
+ describe 'should be overrideable' do
45
+ it 'for site' do
46
+ expect(enterprise.options.client_options.site).to eq(enterprise_site)
47
+ end
48
+
49
+ it 'for authorize url' do
50
+ expect(enterprise.options.client_options.authorize_url).to eq(enterprise_authorize_url)
51
+ end
52
+
53
+ it 'for token url' do
54
+ expect(enterprise.options.client_options.token_url).to eq(enterprise_token_url)
55
+ end
56
+ end
57
+ end
58
+
59
+ describe '#callback_url' do
60
+ let(:base_url) { 'https://example.com' }
61
+
62
+ context 'no script name present' do
63
+ it 'has the correct default callback path' do
64
+ allow(subject).to receive(:full_host) { base_url }
65
+ allow(subject).to receive(:script_name) { '' }
66
+ allow(subject).to receive(:query_string) { '' }
67
+ expect(subject.callback_url).to eq(base_url + '/auth/mattermost/callback')
68
+ end
69
+ end
70
+
71
+ context 'script name' do
72
+ it 'should set the callback path with script_name' do
73
+ allow(subject).to receive(:full_host) { base_url }
74
+ allow(subject).to receive(:script_name) { '/v1' }
75
+ allow(subject).to receive(:query_string) { '' }
76
+ expect(subject.callback_url).to eq(base_url + '/v1/auth/mattermost/callback')
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,16 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+ require 'simplecov'
4
+ SimpleCov.start
5
+ require 'rspec'
6
+ require 'rack/test'
7
+ require 'webmock/rspec'
8
+ require 'omniauth'
9
+ require 'omniauth-mattermost'
10
+
11
+ RSpec.configure do |config|
12
+ config.include WebMock::API
13
+ config.include Rack::Test::Methods
14
+ config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
15
+ end
16
+
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-mattermost
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Quentin Rousseau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
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.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: omniauth-oauth2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '2.0'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.4.0
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '2.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.5'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rack-test
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: webmock
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: Official OmniAuth strategy for Mattermost.
104
+ email:
105
+ - contact@quent.in
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".github/workflows/ruby.yml"
111
+ - ".gitignore"
112
+ - ".rspec"
113
+ - Gemfile
114
+ - Guardfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - lib/omniauth-mattermost.rb
119
+ - lib/omniauth-mattermost/version.rb
120
+ - lib/omniauth/strategies/mattermost.rb
121
+ - omniauth-mattermost.gemspec
122
+ - spec/omniauth/strategies/mattermost_spec.rb
123
+ - spec/spec_helper.rb
124
+ homepage: https://github.com/kwent/omniauth-mattermost
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '3'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubygems_version: 3.5.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Official OmniAuth strategy for Mattermost.
147
+ test_files:
148
+ - spec/omniauth/strategies/mattermost_spec.rb
149
+ - spec/spec_helper.rb