Skip to content

Commit 373203f

Browse files
freegeniePaul Covell
authored and
Paul Covell
committed
Fix issue assaf#17 - Honour token expires_at value
1 parent dbe2eb1 commit 373203f

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/rack/oauth2/models/access_token.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ def from_token(token)
2121
def get_token_for(identity, client, scope, expires = nil)
2222
raise ArgumentError, "Identity must be String or Integer" unless String === identity || Integer === identity
2323
scope = Utils.normalize_scope(scope) & client.scope # Only allowed scope
24-
unless token = collection.find_one({ :identity=>identity, :scope=>scope, :client_id=>client.id, :revoked=>nil })
24+
25+
token = collection.find_one({
26+
:$or=>[{:expires_at=>nil}, {:expires_at=>{:$gt=>Time.now.to_i}}],
27+
:identity=>identity, :scope=>scope,
28+
:client_id=>client.id, :revoked=>nil})
29+
30+
unless token
2531
return create_token_for(client, scope, identity, expires)
2632
end
2733
Server.new_instance self, token

test/oauth/server_methods_test.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,16 @@ def setup
282282
should "return different token for different scope" do
283283
assert @token != Server.token_for("Batman", client.id, %w{read})
284284
end
285-
end
286285

286+
should 'expire token after the specified amount of time' do
287+
Server::AccessToken.collection.drop
288+
token = Server.token_for("Batman", client.id, %w{read write}, 60)
289+
290+
Timecop.travel 120 do
291+
assert token != Server.token_for("Batman", client.id, %w{read write})
292+
end
293+
end
294+
end
287295

288296
context "list access tokens" do
289297
setup do

0 commit comments

Comments
 (0)