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
c4fd3260
Verified
Commit
c4fd3260
authored
3 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Deduplicate error code
parent
455ad071
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/config.rs
+21
-0
21 additions, 0 deletions
src/config.rs
src/nss.rs
+5
-19
5 additions, 19 deletions
src/nss.rs
src/pam.rs
+5
-19
5 additions, 19 deletions
src/pam.rs
with
31 additions
and
38 deletions
src/config.rs
+
21
−
0
View file @
c4fd3260
...
...
@@ -15,6 +15,9 @@
use
serde
::
de
::
Deserialize
;
use
pamsm
::
PamError
;
use
libnss
::
interop
::
Response
;
extern
crate
config
;
const
DEFAULT_CONFIG_FILE
:
&
str
=
"/etc/nss_pam_oidc"
;
...
...
@@ -69,3 +72,21 @@ pub fn get_optional<'de, T: Deserialize<'de>>(conf: &config::Config, key: &str)
},
}
}
enum
DesiredError
{
Response
,
PamError
}
pub
fn
get_or_error
<
'de
,
T
:
Deserialize
<
'de
>>
(
config
:
&
config
::
Config
,
key
:
&
str
,
error_value
:
DesiredError
)
->
Result
<
T
,
Response
>
{
match
get_optional
(
config
,
key
)
{
Some
(
v
)
=>
{
debug!
(
"Configuration key found: {}"
,
key
);
return
Ok
(
v
);
},
None
=>
{
error!
(
"Configuration key not found: {}"
,
key
);
return
Err
(
error_value
);
},
}
}
This diff is collapsed.
Click to expand it.
src/nss.rs
+
5
−
19
View file @
c4fd3260
...
...
@@ -15,14 +15,13 @@
use
crate
::
config
::{
get_config
,
get_optional
get_optional
,
get_or_error
};
use
config
::
Config
;
use
crate
::
logging
::
setup_log
;
use
serde
::
de
::
Deserialize
;
use
oauth2
::{
AuthUrl
,
ClientId
,
...
...
@@ -38,19 +37,6 @@ use oauth2::reqwest::http_client;
use
libnss
::
interop
::
Response
;
use
libnss
::
passwd
::{
PasswdHooks
,
Passwd
};
fn
get_or_nss_error
<
'de
,
T
:
Deserialize
<
'de
>>
(
config
:
&
Config
,
key
:
&
str
)
->
Result
<
T
,
Response
>
{
match
get_optional
(
config
,
key
)
{
Some
(
v
)
=>
{
debug!
(
"Configuration key found: {}"
,
key
);
return
Ok
(
v
);
},
None
=>
{
error!
(
"Configuration key not found: {}"
,
key
);
return
Err
(
Response
::
Unavail
);
},
}
}
fn
nss_hook_prepare
()
->
Config
{
let
conf
=
get_config
(
None
);
...
...
@@ -64,12 +50,12 @@ fn nss_hook_prepare() -> Config {
}
fn
get_bearer_token
(
config
:
Config
)
->
Result
<
String
,
Response
>
{
let
client_id
=
ClientId
::
new
(
get_or_
nss_
error
(
&
config
,
"nss.client_id"
)
?
);
let
client_id
=
ClientId
::
new
(
get_or_error
(
&
config
,
"nss.client_id"
,
Response
::
Unavail
)
?
);
let
client_secret
=
match
get_optional
(
&
config
,
"nss.client_secret"
)
{
Some
(
v
)
=>
Some
(
ClientSecret
::
new
(
v
)),
None
=>
None
,
};
let
auth_url
=
match
AuthUrl
::
new
(
get_or_
nss_
error
(
&
config
,
"nss.auth_url"
)
?
)
{
let
auth_url
=
match
AuthUrl
::
new
(
get_or_error
(
&
config
,
"nss.auth_url"
,
Response
::
Unavail
)
?
)
{
Ok
(
u
)
=>
u
,
_
=>
{
error!
(
"Could not parse authorization URL"
);
...
...
@@ -86,7 +72,7 @@ fn get_bearer_token(config: Config) -> Result<String, Response> {
},
None
=>
None
,
};
let
scopes
:
Vec
<&
str
>
=
get_or_
nss_
error
(
&
config
,
"nss.scopes"
)
?
;
let
scopes
:
Vec
<&
str
>
=
get_or_error
(
&
config
,
"nss.scopes"
,
Response
::
Unavail
)
?
;
let
client
=
BasicClient
::
new
(
client_id
,
client_secret
,
auth_url
,
token_url
);
let
mut
request
=
client
.exchange_client_credentials
();
...
...
This diff is collapsed.
Click to expand it.
src/pam.rs
+
5
−
19
View file @
c4fd3260
...
...
@@ -16,12 +16,11 @@
use
crate
::
config
::{
argv_to_config
,
get_config
,
get_optional
get_optional
,
get_or_error
};
use
config
::
Config
;
use
serde
::
de
::
Deserialize
;
use
crate
::
logging
::
setup_log
;
use
oauth2
::{
...
...
@@ -43,26 +42,13 @@ use oauth2::reqwest::http_client;
use
pamsm
::{
PamServiceModule
,
Pam
,
PamFlag
,
PamError
,
PamLibExt
};
fn
get_or_pam_error
<
'de
,
T
:
Deserialize
<
'de
>>
(
config
:
&
Config
,
key
:
&
str
)
->
Result
<
T
,
PamError
>
{
match
get_optional
(
config
,
key
)
{
Some
(
v
)
=>
{
debug!
(
"Configuration key found: {}"
,
key
);
return
Ok
(
v
);
},
None
=>
{
error!
(
"Configuration key not found: {}"
,
key
);
return
Err
(
PamError
::
SERVICE_ERR
);
},
}
}
fn
do_legacy_auth
(
username
:
String
,
password
:
String
,
config
:
Config
)
->
Result
<
BasicTokenResponse
,
PamError
>
{
let
client_id
=
ClientId
::
new
(
get_or_
pam_
error
(
&
config
,
"pam.client_id"
)
?
);
let
client_id
=
ClientId
::
new
(
get_or_error
(
&
config
,
"pam.client_id"
,
PamError
::
SERVICE_ERR
)
?
);
let
client_secret
=
match
get_optional
(
&
config
,
"pam.client_secret"
)
{
Some
(
v
)
=>
Some
(
ClientSecret
::
new
(
v
)),
None
=>
None
,
};
let
auth_url
=
match
AuthUrl
::
new
(
get_or_
pam_
error
(
&
config
,
"pam.auth_url"
)
?
)
{
let
auth_url
=
match
AuthUrl
::
new
(
get_or_error
(
&
config
,
"pam.auth_url"
,
PamError
::
SERVICE_ERR
)
?
)
{
Ok
(
u
)
=>
u
,
_
=>
{
error!
(
"Could not parse authorization URL"
);
...
...
@@ -79,7 +65,7 @@ fn do_legacy_auth(username: String, password: String, config: Config) -> Result<
},
None
=>
None
,
};
let
scopes
:
Vec
<&
str
>
=
get_or_
pam_
error
(
&
config
,
"pam.scopes"
)
?
;
let
scopes
:
Vec
<&
str
>
=
get_or_error
(
&
config
,
"pam.scopes"
,
PamError
::
SERVICE_ERR
)
?
;
let
res_username
=
ResourceOwnerUsername
::
new
(
username
);
let
res_password
=
ResourceOwnerPassword
::
new
(
password
);
...
...
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