Class: Cafmal::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/cafmal/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url) ⇒ Auth

Returns a new instance of Auth.



13
14
15
# File 'lib/cafmal/auth.rb', line 13

def initialize(api_url)
  @cafmal_api_url = api_url
end

Instance Attribute Details

#cafmal_api_urlObject (readonly)

Returns the value of attribute cafmal_api_url.



11
12
13
# File 'lib/cafmal/auth.rb', line 11

def cafmal_api_url
  @cafmal_api_url
end

#decoded_tokenObject (readonly)

Returns the value of attribute decoded_token.



10
11
12
# File 'lib/cafmal/auth.rb', line 10

def decoded_token
  @decoded_token
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/cafmal/auth.rb', line 9

def token
  @token
end

Instance Method Details

#expired?(force = false) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cafmal/auth.rb', line 17

def expired?(force = false)
  is_expired = false
  if @token.nil?
    is_expired = true
  else
    is_expired = (Time.at(@decoded_token['payload']['exp']).utc.to_datetime < Time.now().utc.to_datetime)
    # force is checking against auth from the api itself instead of relying on JWT exp
    if force
      request_user = JSON.parse(Cafmal::User.new(@cafmal_api_url, @token).show(@decoded_token['payload']['sub']))
      is_expired = request_user.nil?
    end
  end
  return is_expired
end

#login(email = '[email protected]', password = 'cafmal') ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cafmal/auth.rb', line 32

def (email = '[email protected]', password = 'cafmal')
  credentials = {auth: {email: email, password: password}}.to_json
  request_auth = Cafmal::Request::Post.new(@cafmal_api_url + '/user_token', credentials, {"Content-Type" => "application/json"})
  if request_auth.response.code < 300
    @token = JSON.parse(request_auth.response.body)["jwt"]
    @decoded_token = {}
    @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
    @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))

    if (@decoded_token['payload']['role'] != 'worker' && @decoded_token['payload']['role'] != 'alerter')
      team_id = JSON.parse(Cafmal::User.new(@cafmal_api_url, @token).show(@decoded_token['payload']['sub']).body)["team_id"]
      event = Cafmal::Event.new(@cafmal_api_url, @token)
      event.create({name: 'user.login', message: "#{email} has logged in.", kind: 'login', severity: 'info', team_id: team_id})

      #@TODO silence all alerts for your team_id, set silenced_at now + 1h
    end

    true
  end
end

#logout(token) ⇒ Object

we supply the token here, so web does not have to cache the auth obj



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cafmal/auth.rb', line 54

def logout(token)
  headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}"}

  decoded_token = {}
  decoded_token['header'] = JSON.parse(Base64.decode64(token.split('.')[0]))
  decoded_token['payload'] = JSON.parse(Base64.decode64(token.split('.')[1]))

  user = JSON.parse(Cafmal::User.new(@cafmal_api_url, token).show(decoded_token['payload']['sub']).body)
  team_id = user["team_id"]
  email = user["email"]

  # kind has to be login, as it's a label of events
  event_id = JSON.parse(Cafmal::Event.new(@cafmal_api_url, token).create({name: 'user.logout', message: "#{email} has logged out.", kind: 'login', severity: 'info', team_id: team_id}).body)

  if event_id.nil?
    false
  else
    @token = nil
    @decoded_token = nil
    true
  end
end

#refresh(token) ⇒ Object

refresh token



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cafmal/auth.rb', line 78

def refresh(token)
  headers = {"Content-Type" => "application/json", "Authorization" => "Bearer #{token}"}
  credentials = {token: token}.to_json
  request_refresh = Cafmal::Request::Post.new(@cafmal_api_url + '/user_token_refresh', credentials, headers)
  if request_refresh.response.code < 300
    @token = JSON.parse(request_refresh.response.body)['jwt']
    @decoded_token = {}
    @decoded_token['header'] = JSON.parse(Base64.decode64(@token.split('.')[0]))
    @decoded_token['payload'] = JSON.parse(Base64.decode64(@token.split('.')[1]))

    if (@decoded_token['payload']['role'] != 'worker' && @decoded_token['payload']['role'] != 'alerter')
      team_id = JSON.parse(Cafmal::User.new(@cafmal_api_url, @token).show(@decoded_token['payload']['sub']).body)["team_id"]
      event = Cafmal::Event.new(@cafmal_api_url, @token)
      event.create({name: 'user.refresh_login', message: "#{@decoded_token['payload']['email']} has refreshed his login.", kind: 'login', severity: 'info', team_id: team_id})
    end
    return true
  else
    return false
  end
end