Skip to content

Comments

[SQSERVICES-1913] Move OAuth authentication to nginz#3086

Merged
battermann merged 34 commits intoSQSERVICES-1825-be-oauth-refresh-token-generationfrom
SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz
Feb 24, 2023
Merged

[SQSERVICES-1913] Move OAuth authentication to nginz#3086
battermann merged 34 commits intoSQSERVICES-1825-be-oauth-refresh-token-generationfrom
SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz

Conversation

@battermann
Copy link
Contributor

@battermann battermann commented Feb 15, 2023

No description provided.

@battermann battermann changed the base branch from develop to SQSERVICES-1825-be-oauth-refresh-token-generation February 15, 2023 11:07
@zebot zebot added the ok-to-test Approved for running tests in CI, overrides not-ok-to-test if both labels exist label Feb 15, 2023
@battermann battermann changed the title Sqservices 1913 oauth move o auth authentication to nginz [SQSERVICES-1913] Move OAuth authentication to nginz Feb 15, 2023
@battermann battermann force-pushed the SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz branch from eac9bf9 to befeca0 Compare February 21, 2023 14:34

build-release:
cargo build --release
cargo build
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert to release mode

pub struct OAuthJwk(String);

#[repr(C)]
#[derive(Clone, Copy, Debug)]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably don't want to derive Copy here. It's basically an "implicit" clone. And since your struct holds pointer, it could silently copy those pointers and somehow result in double free errors

}
}

impl From<io::Error> for OAuthResultStatus {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably could have handled all these manual From implementations with the thiserror crate.
But it could be tricky to handle with the discriminant so leave it alone if it's too complex

#[repr(C)]
#[derive(Error, Clone, Copy, Debug)]
pub enum OAuthResultStatus {
    Ok,
    IoError(#[from] io::Error),
}

Comment on lines 373 to 377
impl From<str::Utf8Error> for OAuthResultStatus {
fn from(_: str::Utf8Error) -> OAuthResultStatus {
OAuthResultStatus::Utf8Error
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl From<str::Utf8Error> for OAuthResultStatus {
fn from(_: str::Utf8Error) -> OAuthResultStatus {
OAuthResultStatus::Utf8Error
}
}
impl From<str::Utf8Error> for OAuthResultStatus {
fn from(_: str::Utf8Error) -> Self {
Self::Utf8Error
}
}

status: OAuthResultStatus::NullArg,
};
}
if scope.is_null() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't check 'method' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed that, thanks!

Copy link
Contributor

@pcapriotti pcapriotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks generally good, as far as I can tell. I did find a potential problem though. See below.

uid: c_str.into_raw(),
status: OAuthResultStatus::Ok,
}
}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you use catch_unwind_with here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!


static ngx_int_t zauth_token_var_user (ngx_http_request_t * r, ngx_http_variable_value_t * v, uintptr_t _) {
ZauthToken const * t = ngx_http_get_module_ctx(r, zauth_module);
if (t != NULL && zauth_is_authorized_and_allowed(r)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. First t is cast to ZauthToken and assumed to be a valid pointer to that struct by zauth_is_authorized_and_allowed. Later, it is fetched again and cast to a string pointer.

You might want to store a tagged union in the context, so that this function can discriminate if it's the zauth or oauth case.

battermann and others added 8 commits February 22, 2023 14:24
Co-authored-by: Paolo Capriotti <paolo@capriotti.io>
…nz' of github.com:wireapp/wire-server into SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz
…nz' of github.com:wireapp/wire-server into SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz
@battermann battermann marked this pull request as ready for review February 23, 2023 11:47
Copy link
Contributor

@pcapriotti pcapriotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the C code only this time. Looks good to me modulo the comments below.

if (strncmp((char const *) hdr.data, "Bearer ", 7) == 0) {
OAuthResult res = oauth_verify_token(key, &hdr.data[7], hdr.len - 7, scope.data, scope.len, r->method_name.data, r->method_name.len);
if (res.status == OAUTH_OK) {
ZauthContext * ctx = alloc_oauth_context(r, res.uid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For OOM-safety, you can add

if (ctx == NULL) return NGX_ERROR;

or similar.

if (finaliser == NULL) {
return NGX_ERROR;
ZauthContext * ctx = alloc_zauth_context(r, tkn);
ngx_int_t e = setup_zauth_context(r, ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here too.

@battermann battermann merged commit 2af8cf7 into SQSERVICES-1825-be-oauth-refresh-token-generation Feb 24, 2023
@battermann battermann deleted the SQSERVICES-1913-oauth-move-o-auth-authentication-to-nginz branch February 24, 2023 09:29
@battermann battermann mentioned this pull request Feb 24, 2023
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ok-to-test Approved for running tests in CI, overrides not-ok-to-test if both labels exist

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants