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
a4d84959
Verified
Commit
a4d84959
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
[Cache] Implement loading and saving of user token
parent
49da288d
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Cargo.toml
+1
-0
1 addition, 0 deletions
Cargo.toml
src/cache.rs
+60
-3
60 additions, 3 deletions
src/cache.rs
with
61 additions
and
3 deletions
Cargo.toml
+
1
−
0
View file @
a4d84959
...
@@ -25,6 +25,7 @@ serde = "^1.0.125"
...
@@ -25,6 +25,7 @@ serde = "^1.0.125"
log
=
"^0.4.11"
log
=
"^0.4.11"
syslog
=
"^5.0.0"
syslog
=
"^5.0.0"
xdg
=
"^2.2.0"
xdg
=
"^2.2.0"
serde_json
=
"^1.0.64"
[profile.release]
[profile.release]
opt-level
=
'z'
opt-level
=
'z'
...
...
This diff is collapsed.
Click to expand it.
src/cache.rs
+
60
−
3
View file @
a4d84959
...
@@ -26,14 +26,18 @@ use std::ffi::CString;
...
@@ -26,14 +26,18 @@ use std::ffi::CString;
use
oauth2
::
basic
::
BasicTokenResponse
;
use
oauth2
::
basic
::
BasicTokenResponse
;
use
std
::
env
;
use
std
::
env
;
use
std
::
fs
::
remove_file
;
use
std
::
fs
;
use
std
::
io
;
use
std
::
io
;
use
std
::
path
::
PathBuf
;
use
std
::
path
::
PathBuf
;
use
xdg
::{
BaseDirectories
,
BaseDirectoriesError
};
use
xdg
::{
BaseDirectories
,
BaseDirectoriesError
};
use
serde
::{
Deserialize
,
Serialize
};
use
serde_json
;
const
TOKEN_DEFAULT_EXPIRES
:
u64
=
24
*
60
*
60
;
const
TOKEN_DEFAULT_EXPIRES
:
u64
=
24
*
60
*
60
;
const
USER_TOKEN_FILENAME
:
&
str
=
"user_token.json"
;
const
USER_TOKEN_FILENAME
:
&
str
=
"user_token.json"
;
#[derive(Serialize,
Deserialize)]
struct
UserToken
{
struct
UserToken
{
access_token
:
String
,
access_token
:
String
,
expires_at
:
u64
,
expires_at
:
u64
,
...
@@ -128,11 +132,47 @@ impl Cache {
...
@@ -128,11 +132,47 @@ impl Cache {
}
}
pub
fn
load_user_token
(
&
self
,
owner
:
String
)
->
Option
<&
UserToken
>
{
pub
fn
load_user_token
(
&
self
,
owner
:
String
)
->
Option
<&
UserToken
>
{
return
self
.user_tokens
.get
(
&
owner
);
match
self
.user_tokens
.get
(
&
owner
)
{
Some
(
t
)
=>
{
if
t
.is_expired
()
{
// Try to load token from file
self
.drop_privileges
(
owner
)
.ok
();
let
new_token
=
match
self
.place_user_cache_file
(
owner
,
USER_TOKEN_FILENAME
)
{
Ok
(
path
)
=>
match
load_json
(
path
)
{
Ok
(
read_token
)
=>
{
self
.user_tokens
.insert
(
owner
,
read_token
);
Some
(
&
read_token
)
},
Err
(
e
)
=>
{
self
.user_tokens
.remove
(
&
owner
);
None
}
},
Err
(
e
)
=>
{
self
.user_tokens
.remove
(
&
owner
);
None
}
};
self
.restore_privileges
();
new_token
}
else
{
Some
(
t
)
}
},
None
=>
None
}
}
}
pub
fn
save_user_token
(
&
self
,
owner
:
String
,
token
:
UserToken
)
{
pub
fn
save_user_token
(
&
self
,
owner
:
String
,
token
:
UserToken
)
{
self
.user_tokens
.insert
(
owner
,
token
);
self
.user_tokens
.insert
(
owner
,
token
);
// Try to write user's token cache file
self
.drop_privileges
(
owner
)
.ok
();
match
self
.place_user_cache_file
(
owner
,
USER_TOKEN_FILENAME
)
{
Ok
(
path
)
=>
save_json
(
path
,
token
),
Err
(
e
)
=>
Err
(
e
)
};
self
.restore_privileges
();
}
}
pub
fn
delete_user_token
(
&
self
,
owner
:
String
)
{
pub
fn
delete_user_token
(
&
self
,
owner
:
String
)
{
...
@@ -141,7 +181,7 @@ impl Cache {
...
@@ -141,7 +181,7 @@ impl Cache {
// Try to remove user's token cache file
// Try to remove user's token cache file
self
.drop_privileges
(
owner
)
.ok
();
self
.drop_privileges
(
owner
)
.ok
();
match
self
.place_user_cache_file
(
owner
,
USER_TOKEN_FILENAME
)
{
match
self
.place_user_cache_file
(
owner
,
USER_TOKEN_FILENAME
)
{
Ok
(
path
)
=>
remove_file
(
path
),
Ok
(
path
)
=>
fs
::
remove_file
(
path
),
Err
(
e
)
=>
Err
(
e
)
Err
(
e
)
=>
Err
(
e
)
};
};
self
.restore_privileges
();
self
.restore_privileges
();
...
@@ -156,6 +196,23 @@ impl Cache {
...
@@ -156,6 +196,23 @@ impl Cache {
}
}
}
}
fn
load_json
<
'de
,
O
:
Deserialize
<
'de
>>
(
path
:
PathBuf
)
->
Result
<
O
,
io
::
Error
>
{
let
json
=
fs
::
read_to_string
(
path
)
?
;
match
serde_json
::
from_str
(
&
json
)
{
Ok
(
o
)
=>
Ok
(
o
),
Err
(
e
)
=>
return
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidData
,
e
))
}
}
fn
save_json
<
O
:
Serialize
>
(
path
:
PathBuf
,
obj
:
O
)
->
Result
<
(),
io
::
Error
>
{
let
json
=
match
serde_json
::
to_string
(
&
obj
)
{
Ok
(
j
)
=>
j
,
Err
(
e
)
=>
return
Err
(
io
::
Error
::
new
(
io
::
ErrorKind
::
InvalidData
,
e
))
};
fs
::
write
(
path
,
json
)
}
lazy_static!
{
lazy_static!
{
pub
static
ref
CACHE
:
Cache
=
Cache
::
new
();
pub
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