Class: OAuth2::Storages::RedisStrategy

Inherits:
Strategy
  • Object
show all
Defined in:
lib/oauth20/storages/redis_strategy.rb

Constant Summary collapse

@@redis =
nil

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ RedisStrategy

Returns a new instance of RedisStrategy.



10
11
12
# File 'lib/oauth20/storages/redis_strategy.rb', line 10

def initialize(uri)
  @@uri = URI.parse(uri)
end

Instance Method Details

#access_token_find_by_key(key) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/oauth20/storages/redis_strategy.rb', line 37

def access_token_find_by_key(key)
  data = JSON.parse(redis.get("access_token:#{key}"))
  data['expires_at'] = Time.at(data['expires_at'])
  data['created_at'] = Time.at(data['created_at'])
  
  AccessToken.new(symbolize(data))
end

#access_token_save(access_token) ⇒ Object

ACCESS TOKEN



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/oauth20/storages/redis_strategy.rb', line 22

def access_token_save(access_token)
  data = {
    :client_key => access_token.client_key,
    :user_id => access_token.user_id,
    :expires_in => access_token.expires_in,
    :created_at => access_token.created_at.to_i,
    :expires_at => access_token.expires_at.to_i,
    :scope => access_token.scope,
    :key => access_token.key,
    :token_type => access_token.token_type
  }
  
  redis.set "access_token:#{access_token.key}", data.to_json 
end

#auth_code_find_by_key(key) ⇒ Object



62
63
64
65
66
67
# File 'lib/oauth20/storages/redis_strategy.rb', line 62

def auth_code_find_by_key(key)
  data = JSON.parse(redis.get("auth_code:#{key}"))
  data['expires_at'] = Time.at(data['expires_at'])
  data['created_at'] = Time.at(data['created_at'])
  AuthCode.new(symbolize(data))
end

#auth_code_save(auth_code) ⇒ Object

AUTHORIZATION CODE



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/oauth20/storages/redis_strategy.rb', line 49

def auth_code_save(auth_code)
  data = {
    :client_key => auth_code.client_key,
    :user_id => auth_code.user_id,
    :created_at => auth_code.created_at.to_i,
    :expires_at => auth_code.expires_at.to_i,
    :access_token => auth_code.access_token,
    :key => auth_code.key
  }
  
  redis.set "auth_code:#{auth_code.key}", data.to_json
end

#client_allObject



99
100
101
102
103
# File 'lib/oauth20/storages/redis_strategy.rb', line 99

def client_all
  redis.keys("client:*").map! do |key|
    client_find_by_key(key.split(':').last)
  end
end

#client_find_by_key(key) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/oauth20/storages/redis_strategy.rb', line 85

def client_find_by_key(key)
  begin
    data = JSON.parse(redis.get("client:#{key}"))
    Client.new(data.delete('name'), symbolize(data))
  rescue 
    nil
  end
end

#client_find_by_secret(secret) ⇒ Object



94
95
96
97
# File 'lib/oauth20/storages/redis_strategy.rb', line 94

def client_find_by_secret(secret)
  key = redis.get("client_secret:#{secret}:key")
  client_find_by_key(key)
end

#client_save(client) ⇒ Object

CLIENT



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oauth20/storages/redis_strategy.rb', line 73

def client_save(client)
  data = {
    :key => client.key,
    :secret => client.secret,
    :redirect_uri => client.redirect_uri,
    :name => client.name
  }

  redis.set "client:#{client.key}", data.to_json 
  redis.set "client_secret:#{client.secret}:key", client.key
end

#redisObject



14
15
16
# File 'lib/oauth20/storages/redis_strategy.rb', line 14

def redis
  @@redis ||= Redis.new(:host => @@uri.host, :port => @@uri.port, :password => @@uri.password)
end

#user_allObject



123
124
125
126
127
# File 'lib/oauth20/storages/redis_strategy.rb', line 123

def user_all
  redis.keys("user:*").map! do |email|
    user_find_by_email(email.split(':').last)
  end
end

#user_find_by_email(email) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/oauth20/storages/redis_strategy.rb', line 114

def user_find_by_email(email)
  begin
    data = JSON.parse(redis.get("user:#{email}"))
    User.new(symbolize(data))
  rescue 
    nil
  end
end

#user_save(user) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/oauth20/storages/redis_strategy.rb', line 105

def user_save(user)
  data = {
    :email => user.email,
    :password => user.password
  }
  
  redis.set "user:#{user.email}", data.to_json
end