Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
nss-pam-webapi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
magicfelix
nss-pam-webapi
Commits
cf3ec3ed
Verified
Commit
cf3ec3ed
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Implement stubs for cache handling of user tokens
parent
f6189ced
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/cache.rs
+63
-8
63 additions, 8 deletions
src/cache.rs
with
63 additions
and
8 deletions
src/cache.rs
+
63
−
8
View file @
cf3ec3ed
...
@@ -13,20 +13,75 @@
...
@@ -13,20 +13,75 @@
* limitations under the License.
* limitations under the License.
*/
*/
use
lazy_static
::
lazy_static
;
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashMap
;
use
std
::
convert
::
From
;
use
std
::
time
::
SystemTime
;
use
oauth2
::
basic
::
BasicTokenResponse
;
const
TOKEN_DEFAULT_EXPIRES
:
u64
=
24
*
60
*
60
;
struct
UserToken
{
struct
UserToken
{
access_token
:
String
,
access_token
:
String
,
expires_in
:
u32
,
expires_at
:
u64
,
refresh_token
:
String
,
refresh_token
:
Option
<
String
>
,
scope
:
String
,
}
token_type
:
String
,
impl
UserToken
{
fn
is_expired
(
&
self
)
->
bool
{
match
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
{
Ok
(
d
)
=>
d
.as_secs
()
>=
self
.expires_at
,
Err
(
_
)
=>
true
}
}
}
impl
From
<
BasicTokenResponse
>
for
UserToken
{
fn
from
(
response
:
BasicTokenResponse
)
->
Self
{
UserToken
{
access_token
:
response
.access_token
.secret
()
.to_string
(),
expires_at
:
match
response
.expires_in
{
Some
(
duration
)
=>
duration
,
None
=>
TOKEN_DEFAULT_EXPIRES
}
+
SystemTime
::
now
()
.duration_since
(
SystemTime
::
UNIX_EPOCH
)
.ok
()
.unwrap
()
.as_secs
(),
refresh_token
:
match
response
.refresh_token
{
Some
(
t
)
=>
Some
(
t
.secret
()
.to_string
()),
None
=>
None
}
}
}
}
}
static
mut
USER_TOKENS
:
Option
<
HashMap
<
String
,
UserToken
>>
=
None
;
struct
Cache
{
user_tokens
:
HashMap
<
String
,
UserToken
>
}
impl
Cache
{
fn
new
()
->
Cache
{
Cache
{
user_tokens
:
HashMap
::
new
()
}
}
fn
load_user_token
(
&
self
,
owner
:
String
)
->
Option
<&
UserToken
>
{
return
self
.user_tokens
.get
(
&
owner
);
}
fn
setup_cache
()
{
fn
save_user_token
(
&
self
,
owner
:
String
,
token
:
UserToken
)
{
unsafe
{
self
.user_tokens
.insert
(
owner
,
token
);
USER_TOKENS
=
Some
(
HashMap
::
new
());
}
}
fn
cleanup_tokens
(
&
self
)
{
for
(
owner
,
token
)
in
self
.user_tokens
{
if
token
.is_expired
()
{
self
.user_tokens
.remove
(
&
owner
);
}
}
}
}
lazy_static!
{
static
ref
CACHE
:
Cache
=
Cache
::
new
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment