Browse Source

Implement Mattermost authentication

master
Dylan Baker 3 years ago
parent
commit
dbfa1b1085
3 changed files with 80 additions and 0 deletions
  1. 4
    0
      .env.sample
  2. 42
    0
      lib/slack_mattermost_emoji/mattermost_client.rb
  3. 34
    0
      spec/mattermost_client_spec.rb

+ 4
- 0
.env.sample View File

@@ -1,3 +1,7 @@
1 1
 SLACK_DOMAIN=
2 2
 SLACK_USERNAME=
3 3
 SLACK_PASSWORD=
4
+
5
+MATTERMOST_DOMAIN=
6
+MATTERMOST_USERNAME=
7
+MATTERMOST_PASSWORD=

+ 42
- 0
lib/slack_mattermost_emoji/mattermost_client.rb View File

@@ -0,0 +1,42 @@
1
+require 'dotenv/load'
2
+require 'httparty'
3
+require 'json'
4
+
5
+module SlackMattermostEmoji
6
+  class MattermostClient
7
+    def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
8
+      @username = username
9
+      @password = password
10
+      @domain = domain
11
+      @download_path = File.expand_path(download_path || './slack-emojis')
12
+      @token = nil
13
+
14
+      if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
15
+        raise 'Mattermost username, password, and domain are all required'
16
+      end
17
+    end
18
+
19
+    def authenticate
20
+      url = "https://#{@domain}/api/v4/users/login"
21
+      response = HTTParty.post(url, {
22
+        body: JSON.generate({
23
+          login_id: @username,
24
+          password: @password,
25
+        }),
26
+        headers: {
27
+          'Content-Type' => 'application/json'
28
+        }
29
+      })
30
+
31
+      if response.code == 401
32
+        raise 'Invalid Mattermost credentials'
33
+      end
34
+
35
+      @token = response.headers['token']
36
+    end
37
+
38
+    def authenticated?
39
+      !@token.nil?
40
+    end
41
+  end
42
+end

+ 34
- 0
spec/mattermost_client_spec.rb View File

@@ -0,0 +1,34 @@
1
+require "dotenv/load"
2
+require "slack_mattermost_emoji/mattermost_client"
3
+
4
+RSpec.describe SlackMattermostEmoji::MattermostClient do
5
+  it 'should raise if credentials are missing' do
6
+    expect do
7
+       SlackMattermostEmoji::MattermostClient.new(
8
+        username: '',
9
+        password: '',
10
+        domain: '',
11
+      )
12
+    end.to raise_error('Mattermost username, password, and domain are all required')
13
+  end
14
+
15
+  it 'should be able to log in to mattermost using the provided credentials' do
16
+    client = SlackMattermostEmoji::MattermostClient.new(
17
+      username: ENV['MATTERMOST_USERNAME'],
18
+      password: ENV['MATTERMOST_PASSWORD'],
19
+      domain: ENV['MATTERMOST_DOMAIN'],
20
+    )
21
+    client.authenticate
22
+
23
+    expect(client.authenticated?).to be true
24
+  end
25
+
26
+  it 'should handle a bad credentials error' do
27
+    client = SlackMattermostEmoji::MattermostClient.new(
28
+      username: ENV['MATTERMOST_USERNAME'],
29
+      password: 'wrongpassword',
30
+      domain: ENV['MATTERMOST_DOMAIN'],
31
+    )
32
+    expect { client.authenticate }.to raise_error('Invalid Mattermost credentials')
33
+  end
34
+end

Loading…
Cancel
Save