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
1
Merge Requests
1
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
nexedi
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
end
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?
encrypted_value
=
Gitlab
::
CryptoHelper
.
aes256_gcm_encrypt
(
token
)
token_authenticatable
=
relation
(
unscoped
)
.
find_by
(
encrypted_field
=>
encrypted_value
)
end
if
fallback?
||
migrating?
token_authenticatable
||=
fallback_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
)
if
fallback?
find_by_encrypted_token
(
token
,
unscoped
)
||
find_by_plaintext_token
(
token
,
unscoped
)
elsif
migrating?
find_by_plaintext_token
(
token
,
unscoped
)
||
find_by_encrypted_token
(
token
,
unscoped
)
else
raise
ArgumentError
,
'Unknown encryption strategy!'
end
token_authenticatable
end
def
ensure_token
(
instance
)
...
...
@@ -47,20 +39,20 @@ module TokenAuthenticatableStrategies
return
super
if
instance
.
has_attribute?
(
encrypted_field
)
if
f
allback
?
fallback_strategy
.
ensure_token
(
instance
)
if
f
ully_encrypted
?
raise
ArgumentError
,
'Using encrypted strategy when encrypted field is missing!'
else
raise
ArgumentError
,
'No fallback defined when encrypted field is missing!'
insecure_strategy
.
ensure_token
(
instance
)
end
end
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
)
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
def
set_token
(
instance
,
token
)
...
...
@@ -72,16 +64,29 @@ module TokenAuthenticatableStrategies
token
end
def
fully_encrypted?
!
migrating?
&&
!
fallback?
end
protected
def
fallback_strategy
@fallback_strategy
||=
TokenAuthenticatableStrategies
::
Insecure
def
find_by_plaintext_token
(
token
,
unscoped
)
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
)
end
def
token_set?
(
instance
)
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?
end
...
...
spec/models/concerns/token_authenticatable_strategies/encrypted_spec.rb
View file @
e9abaced
...
...
@@ -35,8 +35,8 @@ describe TokenAuthenticatableStrategies::Encrypted do
.
to
eq
'encrypted resource'
end
it
'uses
fallback
strategy when encrypted token cannot be found'
do
allow
(
subject
.
send
(
:
fallback
_strategy
))
it
'uses
insecure
strategy when encrypted token cannot be found'
do
allow
(
subject
.
send
(
:
insecure
_strategy
))
.
to
receive
(
:find_token_authenticatable
)
.
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