Class: Rack::OAuth2::Server::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/oauth2/models/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#_idObject (readonly) Also known as: id

Client identifier.



73
74
75
# File 'lib/rack/oauth2/models/client.rb', line 73

def _id
  @_id
end

#created_atObject (readonly)

Does what it says on the label.



91
92
93
# File 'lib/rack/oauth2/models/client.rb', line 91

def created_at
  @created_at
end

#display_nameObject (readonly)

User see this.



78
79
80
# File 'lib/rack/oauth2/models/client.rb', line 78

def display_name
  @display_name
end

#image_urlObject (readonly)

Preferred image URL for this icon.



82
83
84
# File 'lib/rack/oauth2/models/client.rb', line 82

def image_url
  @image_url
end

Link to client’s Web site.



80
81
82
# File 'lib/rack/oauth2/models/client.rb', line 80

def link
  @link
end

#notesObject (readonly)

Free form fields for internal use.



89
90
91
# File 'lib/rack/oauth2/models/client.rb', line 89

def notes
  @notes
end

#redirect_uriObject (readonly)

Redirect URL. Supplied by the client if they want to restrict redirect URLs (better security).



85
86
87
# File 'lib/rack/oauth2/models/client.rb', line 85

def redirect_uri
  @redirect_uri
end

#revokedObject

Timestamp if revoked.



93
94
95
# File 'lib/rack/oauth2/models/client.rb', line 93

def revoked
  @revoked
end

#scopeObject (readonly)

List of scope the client is allowed to request.



87
88
89
# File 'lib/rack/oauth2/models/client.rb', line 87

def scope
  @scope
end

#secretObject (readonly)

Client secret: random, long, and hexy.



76
77
78
# File 'lib/rack/oauth2/models/client.rb', line 76

def secret
  @secret
end

#tokens_grantedObject (readonly)

Counts how many access tokens were granted.



95
96
97
# File 'lib/rack/oauth2/models/client.rb', line 95

def tokens_granted
  @tokens_granted
end

#tokens_revokedObject (readonly)

Counts how many access tokens were revoked.



97
98
99
# File 'lib/rack/oauth2/models/client.rb', line 97

def tokens_revoked
  @tokens_revoked
end

Class Method Details

.allObject

Returns all the clients in the database, sorted alphabetically.



53
54
55
56
# File 'lib/rack/oauth2/models/client.rb', line 53

def all
  collection.find({}, { :sort=>[[:display_name, Mongo::ASCENDING]] }).
    map { |fields| Server.new_instance self, fields }
end

.collectionObject



67
68
69
# File 'lib/rack/oauth2/models/client.rb', line 67

def collection
  Server.database["oauth2.clients"]
end

.create(args) ⇒ Object

Create a new client. Client provides the following properties: # :display_name – Name to show (e.g. UberClient) # :link – Link to client Web site (e.g. uberclient.dot) # :image_url – URL of image to show alongside display name # :redirect_uri – Registered redirect URI. # :scope – List of names the client is allowed to request. # :notes – Free form text.

This method does not validate any of these fields, in fact, you’re not required to set them, use them, or use them as suggested. Using them as suggested would result in better user experience. Don’t ask how we learned that.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/oauth2/models/client.rb', line 27

def create(args)
  redirect_uri = Server::Utils.parse_redirect_uri(args[:redirect_uri]).to_s if args[:redirect_uri]
  scope = Server::Utils.normalize_scope(args[:scope])
  fields =  { :display_name=>args[:display_name], :link=>args[:link],
              :image_url=>args[:image_url], :redirect_uri=>redirect_uri,
              :nodes=>args[:notes].to_s, :scope=>scope,
              :created_at=>Time.now.to_i, :revoked=>nil }
  if args[:id] && args[:secret]
    fields[:_id], fields[:secret] = BSON::ObjectId(args[:id].to_s), args[:secret]
    collection.insert(fields, :safe=>true)
  else
    fields[:secret] = Server.secure_random
    fields[:_id] = collection.insert(fields)
  end
  Server.new_instance self, fields
end

.delete(client_id) ⇒ Object

Deletes client with given identifier (also, all related records).



59
60
61
62
63
64
65
# File 'lib/rack/oauth2/models/client.rb', line 59

def delete(client_id)
  id = BSON::ObjectId(client_id.to_s)
  Client.collection.remove({ :_id=>id })
  AuthRequest.collection.remove({ :client_id=>id })
  AccessGrant.collection.remove({ :client_id=>id })
  AccessToken.collection.remove({ :client_id=>id })
end

.find(client_id) ⇒ Object

Authenticate a client request. This method takes three arguments, Find Client from client identifier.



10
11
12
13
# File 'lib/rack/oauth2/models/client.rb', line 10

def find(client_id)
  id = BSON::ObjectId(client_id.to_s)
  Server.new_instance self, collection.find_one(id)
end

.lookup(field) ⇒ Object

Lookup client by ID, display name or URL.



45
46
47
48
49
50
# File 'lib/rack/oauth2/models/client.rb', line 45

def lookup(field)
  id = BSON::ObjectId(field.to_s)
  Server.new_instance self, collection.find_one(id)
rescue BSON::InvalidObjectId
  Server.new_instance self, collection.find_one({ :display_name=>field }) || collection.find_one({ :link=>field })
end

Instance Method Details

#revoke!Object

Revoke all authorization requests, access grants and access tokens for this client. Ward off the evil.



101
102
103
104
105
106
107
# File 'lib/rack/oauth2/models/client.rb', line 101

def revoke!
  self.revoked = Time.now.to_i
  Client.collection.update({ :_id=>id }, { :$set=>{ :revoked=>revoked } })
  AuthRequest.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } })
  AccessGrant.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } })
  AccessToken.collection.update({ :client_id=>id }, { :$set=>{ :revoked=>revoked } })
end

#update(args) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/rack/oauth2/models/client.rb', line 109

def update(args)
  fields = [:display_name, :link, :image_url, :notes].inject({}) { |h,k| v = args[k]; h[k] = v if v; h }
  fields[:redirect_uri] = Server::Utils.parse_redirect_uri(args[:redirect_uri]).to_s if args[:redirect_uri]
  fields[:scope] = Server::Utils.normalize_scope(args[:scope])
  self.class.collection.update({ :_id=>id }, { :$set=>fields })
  self.class.find(id)
end