Giter Club home page Giter Club logo

oauth1-request-rs's Introduction

oauth1-request

Build Status Current Version Documentation

Yet yet yet another OAuth 1.0 client library for Rust.

Usage

Add this to your Cargo.toml:

[dependencies]
oauth = { version = "0.6", package = "oauth1-request" }

A typical authorization flow looks like this:

// Define a type to represent your request.
#[derive(oauth::Request)]
struct CreateComment<'a> {
    article_id: u64,
    text: &'a str,
}

let uri = "https://example.com/api/v1/comments/create.json";

let request = CreateComment {
    article_id: 123456789,
    text: "A request signed with OAuth & Rust ๐Ÿฆ€ ๐Ÿ”",
};

// Prepare your credentials.
let token =
    oauth::Token::from_parts("consumer_key", "consumer_secret", "token", "token_secret");

// Create the `Authorization` header.
let authorization_header = oauth::post(uri, &request, &token, oauth::HmacSha1);
// `oauth_nonce` and `oauth_timestamp` vary on each execution.
assert_eq!(
    authorization_header,
    "OAuth \
         oauth_consumer_key=\"consumer_key\",\
         oauth_nonce=\"Dk-OGluFEQ4f\",\
         oauth_signature_method=\"HMAC-SHA1\",\
         oauth_timestamp=\"1234567890\",\
         oauth_token=\"token\",\
         oauth_signature=\"n%2FrUgos4CFFZbZK8Z8wFR7drU4c%3D\"",
);

// You can create an `x-www-form-urlencoded` string or a URI with query pairs from the request.

let form = oauth::to_form(&request);
assert_eq!(
    form,
    "article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);

let uri = oauth::to_query(uri.to_owned(), &request);
assert_eq!(
    uri,
    "https://example.com/api/v1/comments/create.json?article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);

oauth1-request-rs's People

Contributors

aaron1011 avatar dbw9580 avatar eddyb avatar tesaguri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

oauth1-request-rs's Issues

Replace `Request` with plain `String` and make query serialization optional

Request.data can have either of a URI with query parameters or a form-urlencoded string. A single field having two different meaning depending on context is error-prone.

Also, the field actually has nothing to do with the OAuth authorization process. It was introduced just as a convenience and not all users require it, spending cost for unnecessary feature.

For these reasons, I'm planning to separate the query serialization from the Authorize-ation process and make Signer return a single "Authorization" header String. So, I'm going to change this:

extern crate oauth1_request as oauth;

let mut builder = oauth::Builder::new(client, oauth::HmacSha1);
builder.token(token);

let req = Foo { a: 1 };

let oauth::Request { authorization, data } = builder.post_form("https://example.com/", &req);
assert_eq!(data, "a=1");

let oauth::Request { authorization, data } = builder.get("https://example.com/", &req);
assert_eq!(data, "https://example.com/?a=1");

to something like this:

extern crate oauth1_request as oauth;
extern crate url;

let mut builder = oauth::Builder::new(client, oauth::HmacSha1);
builder.token(token);

let req = Foo { a: 1 };

let mut url: url::Url = "https://example.com/".parse().unwrap();

// N.B. we now call this method `post` instead of `post_form`
// since the method no longer generates a form-urlencoded string.
let authorization: String = builder.post("https://example.com/", &req);
let form = oauth::make_form_urlencoded(&req);
assert_eq!(form, "a=1");

let authorization: String = builder.get(&url, &req);
oauth::make_query(&req, url.query_pairs_mut());
assert_eq!(url.as_str(), "https://example.com/?a=1");

The concrete interface for oauth::make_{form_urlencoded,query} above is yet to be decided. There a few possibility for them, including:

  1. Using another crate like serde_urlencoded

  2. Make Signer generic over a "target" type. For example, Signer<Authorization> (where Authorization is a unit struct) would create an "Authorization" header string, and Signer<url::form_urlencoded::Serializer<_>> would append query pairs to the underlying Serializer, ignoring any OAuth parameters

Naming of `Token` type

While the term "token" in OAuth 1.0 refers to token credentials, oauth_credentials::Token represents a set of client credentials and token credentials, which are not identical concepts. The difference is confusing especially when a user uses the term to refer to the both concepts at the same time, which is the case almost every time they construct a Token.

let token: Credentials = get_token(&client).await; // Token credentials
let token = Token::new(client, token); // Client and token credentials

Also, token.client() and token.token() doesn't sound very well.

So I think Token should be renamed if there is a good alternative. Renaming a type doesn't prevent the semver trick, so it wouldn't cause a breakage provided that it is done before 1.0 release.

Best way to serialize keywords/reserved words

I need to create a query string from, e.g.:

#[derive(oauth1_request::Request)]
struct TextQuery {
    r#type: String,
}

But oauth1_request::to_uri_query(uri, &request_query) produces ?r#type=text (note r#). With serde you would use #[serde(rename = "type")], but it's not clear how to best go about doing it here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.