Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make auth interface more generic #26

Merged
merged 2 commits into from
Nov 11, 2021

Conversation

miraclx
Copy link
Contributor

@miraclx miraclx commented Nov 11, 2021

This makes the auth interface more generic, primarily to extend the flexibility of custom implementations. Allowing them to be able to customize their header names.

use near_jsonrpc_client::auth;

struct CustomUserAuth {
    username: String,
    password: zeroize::Zeroizing<String>
}

impl auth::AuthScheme for CustomUserAuth {
    fn get_auth_header(&self) -> auth::AuthHeaderEntry {
        auth::AuthHeaderEntry {
            header: "Authorization".to_string(),
            token: format!("Basic {}", base64::encode(format!("{}:{}", self.username, *self.password))),
        }
    }
}

let client = JsonRpcClient::connect("http://localhost:3030")
    .auth(CustomUserAuth { username: "root", password: "n1c3_try".to_string().into() });

Also replaces Authorization: Basic <token> with x-api-key: <token>.

let client = JsonRpcClient::connect("http://localhost:3030")
    .auth(BasicAuth { token: "our-secret-token" });

Now becomes;

  1.  let client = JsonRpcClient::connect("http://localhost:3030")
         .auth(ApiKey::Plain("our-secret-token"));

    - or -

  2.  let client = JsonRpcClient::connect("http://localhost:3030")
         .auth(ApiKey::Base64("b3VyLXNlY3JldC10b2tlbg=="));

src/auth.rs Outdated Show resolved Hide resolved
@miraclx miraclx requested review from frol and chefsale November 11, 2021 03:15
Copy link

@chefsale chefsale left a comment

Choose a reason for hiding this comment

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

Good stuff

@miraclx miraclx merged commit ec15b4d into master Nov 11, 2021
@miraclx miraclx deleted the impl-more-generic-authentication branch November 11, 2021 04:27
@frol frol mentioned this pull request Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants