Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
e9abaced
Commit
e9abaced
authored
Dec 03, 2018
by
Grzegorz Bizon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor encrypted token strategy class
parent
fe4b5c98
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
28 deletions
+33
-28
app/models/concerns/token_authenticatable_strategies/encrypted.rb
...ls/concerns/token_authenticatable_strategies/encrypted.rb
+31
-26
spec/models/concerns/token_authenticatable_strategies/encrypted_spec.rb
...ncerns/token_authenticatable_strategies/encrypted_spec.rb
+2
-2
No files found.
app/models/concerns/token_authenticatable_strategies/encrypted.rb
View file @
e9abaced
...
@@ -11,26 +11,18 @@ module TokenAuthenticatableStrategies
...
@@ -11,26 +11,18 @@ module TokenAuthenticatableStrategies
end
end
def
find_token_authenticatable
(
token
,
unscoped
=
false
)
def
find_token_authenticatable
(
token
,
unscoped
=
false
)
return
unless
token
return
if
token
.
blank?
return
find_by_encrypted_token
(
token
,
unscoped
)
if
fully_encrypted?
unless
migrating?
if
fallback?
encrypted_value
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
token
)
find_by_encrypted_token
(
token
,
unscoped
)
||
token_authenticatable
=
relation
(
unscoped
)
find_by_plaintext_token
(
token
,
unscoped
)
.
find_by
(
encrypted_field
=>
encrypted_value
)
elsif
migrating?
end
find_by_plaintext_token
(
token
,
unscoped
)
||
find_by_encrypted_token
(
token
,
unscoped
)
if
fallback?
||
migrating?
else
token_authenticatable
||=
fallback_strategy
raise
ArgumentError
,
'Unknown encryption strategy!'
.
find_token_authenticatable
(
token
)
end
if
migrating?
encrypted_value
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
token
)
token_authenticatable
||=
relation
(
unscoped
)
.
find_by
(
encrypted_field
=>
encrypted_value
)
end
end
token_authenticatable
end
end
def
ensure_token
(
instance
)
def
ensure_token
(
instance
)
...
@@ -47,20 +39,20 @@ module TokenAuthenticatableStrategies
...
@@ -47,20 +39,20 @@ module TokenAuthenticatableStrategies
return
super
if
instance
.
has_attribute?
(
encrypted_field
)
return
super
if
instance
.
has_attribute?
(
encrypted_field
)
if
f
allback
?
if
f
ully_encrypted
?
fallback_strategy
.
ensure_token
(
instance
)
raise
ArgumentError
,
'Using encrypted strategy when encrypted field is missing!'
else
else
raise
ArgumentError
,
'No fallback defined when encrypted field is missing!'
insecure_strategy
.
ensure_token
(
instance
)
end
end
end
end
def
get_token
(
instance
)
def
get_token
(
instance
)
return
fallback
_strategy
.
get_token
(
instance
)
if
migrating?
return
insecure
_strategy
.
get_token
(
instance
)
if
migrating?
encrypted_token
=
instance
.
read_attribute
(
encrypted_field
)
encrypted_token
=
instance
.
read_attribute
(
encrypted_field
)
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_decrypt
(
encrypted_token
)
token
=
Gitlab
::
CryptoHelper
.
aes256_gcm_decrypt
(
encrypted_token
)
token
||
(
fallback
_strategy
.
get_token
(
instance
)
if
fallback?
)
token
||
(
insecure
_strategy
.
get_token
(
instance
)
if
fallback?
)
end
end
def
set_token
(
instance
,
token
)
def
set_token
(
instance
,
token
)
...
@@ -72,16 +64,29 @@ module TokenAuthenticatableStrategies
...
@@ -72,16 +64,29 @@ module TokenAuthenticatableStrategies
token
token
end
end
def
fully_encrypted?
!
migrating?
&&
!
fallback?
end
protected
protected
def
fallback_strategy
def
find_by_plaintext_token
(
token
,
unscoped
)
@fallback_strategy
||=
TokenAuthenticatableStrategies
::
Insecure
insecure_strategy
.
find_token_authenticatable
(
token
,
unscoped
)
end
def
find_by_encrypted_token
(
token
,
unscoped
)
encrypted_value
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
token
)
relation
(
unscoped
).
find_by
(
encrypted_field
=>
encrypted_value
)
end
def
insecure_strategy
@insecure_strategy
||=
TokenAuthenticatableStrategies
::
Insecure
.
new
(
klass
,
token_field
,
options
)
.
new
(
klass
,
token_field
,
options
)
end
end
def
token_set?
(
instance
)
def
token_set?
(
instance
)
raw_token
=
instance
.
read_attribute
(
encrypted_field
)
raw_token
=
instance
.
read_attribute
(
encrypted_field
)
raw_token
||=
(
fallback
_strategy
.
get_token
(
instance
)
if
fallback?
)
raw_token
||=
(
insecure
_strategy
.
get_token
(
instance
)
if
fallback?
)
raw_token
.
present?
raw_token
.
present?
end
end
...
...
spec/models/concerns/token_authenticatable_strategies/encrypted_spec.rb
View file @
e9abaced
...
@@ -35,8 +35,8 @@ describe TokenAuthenticatableStrategies::Encrypted do
...
@@ -35,8 +35,8 @@ describe TokenAuthenticatableStrategies::Encrypted do
.
to
eq
'encrypted resource'
.
to
eq
'encrypted resource'
end
end
it
'uses
fallback
strategy when encrypted token cannot be found'
do
it
'uses
insecure
strategy when encrypted token cannot be found'
do
allow
(
subject
.
send
(
:
fallback
_strategy
))
allow
(
subject
.
send
(
:
insecure
_strategy
))
.
to
receive
(
:find_token_authenticatable
)
.
to
receive
(
:find_token_authenticatable
)
.
and_return
(
'plaintext resource'
)
.
and_return
(
'plaintext resource'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment