-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Parrish
committed
Jul 31, 2015
1 parent
d296f8d
commit 873b3e5
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,28 @@ | ||
class OauthAccessToken < ActiveRecord::Base | ||
belongs_to :resource_owner, class_name: 'User' | ||
|
||
class ExpiredError < StandardError | ||
def message; 'Access Token has expired'; end | ||
alias_method :to_s, :message | ||
end | ||
|
||
class RevokedError < StandardError | ||
def message; 'Access Token has been revoked'; end | ||
alias_method :to_s, :message | ||
end | ||
|
||
def resource_owner | ||
super.tap do |user| | ||
raise ExpiredError if expired? | ||
raise RevokedError if revoked? | ||
end | ||
end | ||
|
||
def expired? | ||
Time.now.utc > created_at + expires_in.seconds | ||
end | ||
|
||
def revoked? | ||
!!revoked_at | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe OauthAccessToken, type: :model do | ||
describe '#expired?' do | ||
let(:token){ create :oauth_access_token } | ||
let(:expired_token){ create :oauth_access_token, :expired } | ||
|
||
it 'should be true for expired tokens' do | ||
expect(expired_token).to be_expired | ||
end | ||
|
||
it 'should be false for valid tokens' do | ||
expect(token).to_not be_expired | ||
end | ||
end | ||
|
||
describe '#revoked?' do | ||
let(:token){ create :oauth_access_token } | ||
let(:revoked_token){ create :oauth_access_token, :revoked } | ||
|
||
it 'should be true for revoked tokens' do | ||
expect(revoked_token).to be_revoked | ||
end | ||
|
||
it 'should be false for valid tokens' do | ||
expect(token).to_not be_revoked | ||
end | ||
end | ||
|
||
describe '#resource_owner' do | ||
let(:token){ create :oauth_access_token } | ||
let(:expired_token){ create :oauth_access_token, :expired } | ||
let(:revoked_token){ create :oauth_access_token, :revoked } | ||
|
||
it 'should not raise anything with a valid token' do | ||
expect{ token.resource_owner }.to_not raise_error | ||
end | ||
|
||
it 'should raise if expired' do | ||
expect{ expired_token.resource_owner }.to raise_error OauthAccessToken::ExpiredError | ||
end | ||
|
||
it 'should raise if revoked' do | ||
expect{ revoked_token.resource_owner }.to raise_error OauthAccessToken::RevokedError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters