forked from jcs/rubywarden
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.rb
51 lines (44 loc) · 1.79 KB
/
device.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
# Copyright (c) 2017 joshua stein <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
class Device < DBModel
self.table_name = "devices"
#set_primary_key "uuid"
before_create :generate_uuid_primary_key
belongs_to :user, foreign_key: :user_uuid, inverse_of: :devices
DEFAULT_TOKEN_VALIDITY = (60 * 60)
def regenerate_tokens!(validity = DEFAULT_TOKEN_VALIDITY)
if self.refresh_token.blank?
self.refresh_token = SecureRandom.urlsafe_base64(64)[0, 64]
end
self.token_expires_at = Time.now + validity
# the official clients parse this JWT and checks for the existence of some
# of these fields
self.access_token = Bitwarden::Token.sign({
:nbf => (Time.now - (60 * 2)).to_i,
:exp => self.token_expires_at.to_i,
:iss => IDENTITY_BASE_URL,
:sub => self.user.uuid,
:premium => self.user.premium,
:name => self.user.name,
:email => self.user.email,
:email_verified => self.user.email_verified,
:sstamp => self.user.security_stamp,
:device => self.uuid,
:scope => [ "api", "offline_access" ],
:amr => [ "Application" ],
})
end
end