Class: Jerakia::Server::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/jerakia/server/auth.rb,
lib/jerakia/server/auth/token.rb

Defined Under Namespace

Classes: Token

Class Method Summary collapse

Class Method Details

.authenticate(token_string) ⇒ Object


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jerakia/server/auth.rb', line 58

def authenticate(token_string)
  api_id, token = token_string.split(/:/)
  entry = get_entry(api_id)
  return false if entry.nil?
  if entry.token == token and entry.active
    seen!(api_id)
    true
  else
    false
  end
end

.create(api_id) ⇒ Object

Raises:


46
47
48
49
50
51
52
# File 'lib/jerakia/server/auth.rb', line 46

def create(api_id)
  raise Jerakia::Error, "API ID #{api_id} already exists" if exists?(api_id)
  token = generate_token
  entry = Jerakia::Server::Auth::Token.new(:api_id => api_id, :token => token)
  entry.save
  api_id + ":" + token
end

.destroy(api_id) ⇒ Object


37
38
39
40
# File 'lib/jerakia/server/auth.rb', line 37

def destroy(api_id)
  entry = get_entry(api_id)
  entry.destroy
end

.disable(api_id) ⇒ Object


29
30
31
# File 'lib/jerakia/server/auth.rb', line 29

def disable(api_id)
  update(api_id, { :active => false })
end

.enable(api_id) ⇒ Object


33
34
35
# File 'lib/jerakia/server/auth.rb', line 33

def enable(api_id)
  update(api_id, { :active => true })
end

.exists?(api_id) ⇒ Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/jerakia/server/auth.rb', line 42

def exists?(api_id)
  get_entry(api_id)
end

.generate_tokenObject


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

def generate_token
  SecureRandom.hex(40)
end

.get_entry(api_id) ⇒ Object


15
16
17
# File 'lib/jerakia/server/auth.rb', line 15

def get_entry(api_id)
  Jerakia::Server::Auth::Token.get(api_id)
end

.get_tokensObject


54
55
56
# File 'lib/jerakia/server/auth.rb', line 54

def get_tokens
  Jerakia::Server::Auth::Token.find
end

.seen!(api_id) ⇒ Object


25
26
27
# File 'lib/jerakia/server/auth.rb', line 25

def seen!(api_id)
  update(api_id, { :last_seen => DateTime.now })
end

.update(api_id, fields) ⇒ Object


19
20
21
22
23
# File 'lib/jerakia/server/auth.rb', line 19

def update(api_id, fields)
  entry = get_entry(api_id)
  entry.update(fields)
  entry.save
end