Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created January 17, 2012 20:12
Show Gist options
  • Save jimsynz/1628625 to your computer and use it in GitHub Desktop.
Save jimsynz/1628625 to your computer and use it in GitHub Desktop.
class TokenExtension
def incoming(m,c)
if m['channel'] == '/service/requestToken'
token = if m['ext'] && m['ext']['token']
Token.find_by_token(m['ext']['token'])
elsif by_client = Token.find_by_client_id(m['clientId'])
by_client
else
Token.new.tap { |t| t.client_id = m['clientId'] }
end
token.refresh if token.is_older_than 30.seconds
end
c.call(m)
end
def outgoing(m,c)
if m['channel'] == '/service/requestToken'
token = Token.find_by_client_id(m['clientId'])
if token && token.valid?
m['ext']['token'] = token.to_message
else
m['error'] = "Permission denied."
end
end
c.call(m)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment