oidc_util/grpc/
authenticator.rs

1use alcoholic_jwt::{JWKS, ValidJWT};
2use tonic::metadata::MetadataMap;
3use tonic::Status;
4use crate::security::validator::validate_token;
5
6/// Authenticates a user by extracting the authorization token from the request metadata
7pub fn authenticate(metadata: &MetadataMap, jwks: &JWKS, issuer_uri: &str) -> Result<ValidJWT, Status> {
8    let bearer_token = match metadata.get("authorization") {
9        None => return Err(Status::unauthenticated("authorization header not provided")),
10        Some(authorization) => {
11            authorization.to_str()
12                .map_err(|e| Status::unauthenticated(e.to_string()))?
13        }
14    };
15
16    validate_token(bearer_token, jwks, issuer_uri)
17        .map_err(|e1| Status::unauthenticated(e1.to_string()))
18}