mirror of
https://github.com/tribufu/sdk-rust
synced 2025-06-16 03:04:19 +00:00
Add oauth2 types
This commit is contained in:
42
src/lib.rs
42
src/lib.rs
@ -3,12 +3,15 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
use alnilam_consts::TARGET_TRIPLE;
|
use alnilam_consts::TARGET_TRIPLE;
|
||||||
use anyhow::Result;
|
use anyhow::{Error, Result};
|
||||||
use reqwest::header::{HeaderMap, HeaderValue};
|
use reqwest::header::{HeaderMap, HeaderValue};
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|
||||||
|
pub mod oauth2;
|
||||||
|
pub mod token;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct TribufuClient {
|
pub struct TribufuClient {
|
||||||
client_id: u64,
|
client_id: u64,
|
||||||
@ -17,7 +20,7 @@ pub struct TribufuClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TribufuClient {
|
impl TribufuClient {
|
||||||
const BASE_URL: &'static str = "https://api.tribufu.com";
|
const BASE_URL: &'static str = "http://localhost:5000";
|
||||||
|
|
||||||
pub fn new(id: u64, secret: impl Into<String>) -> Result<TribufuClient> {
|
pub fn new(id: u64, secret: impl Into<String>) -> Result<TribufuClient> {
|
||||||
let user_agent = format!(
|
let user_agent = format!(
|
||||||
@ -44,6 +47,41 @@ impl TribufuClient {
|
|||||||
pub fn id(&self) -> u64 {
|
pub fn id(&self) -> u64 {
|
||||||
self.client_id
|
self.client_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_token(&self) -> Result<oauth2::OAuth2TokenResponse> {
|
||||||
|
let body = oauth2::OAuth2TokenRequest {
|
||||||
|
grant_type: oauth2::OAuth2GrantType::ClientCredentials,
|
||||||
|
code: None,
|
||||||
|
refresh_token: None,
|
||||||
|
username: None,
|
||||||
|
password: None,
|
||||||
|
client_id: self.client_id.to_string(),
|
||||||
|
client_secret: self.client_secret.clone(),
|
||||||
|
redirect_uri: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = match self
|
||||||
|
.http
|
||||||
|
.post(format!("{}/v1/oauth2/token", Self::BASE_URL))
|
||||||
|
.form(&body)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if response.status() != 200 {
|
||||||
|
return Err(Error::msg(format!(
|
||||||
|
"Failed to get token: {}",
|
||||||
|
response.status()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let token = response.json::<oauth2::OAuth2TokenResponse>().await?;
|
||||||
|
|
||||||
|
Ok(token)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
148
src/oauth2.rs
Normal file
148
src/oauth2.rs
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2ResponseType {
|
||||||
|
Code,
|
||||||
|
Token,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2ClientType {
|
||||||
|
Confidential,
|
||||||
|
Public,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2TokenHintType {
|
||||||
|
AccessToken,
|
||||||
|
RefreshToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2GrantType {
|
||||||
|
AuthorizationCode,
|
||||||
|
ClientCredentials,
|
||||||
|
DeviceCode,
|
||||||
|
Password,
|
||||||
|
RefreshToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2AuthorizeError {
|
||||||
|
AccessDenied,
|
||||||
|
InvalidRequest,
|
||||||
|
InvalidScope,
|
||||||
|
ServerError,
|
||||||
|
TemporarilyUnavailable,
|
||||||
|
UnauthorizedClient,
|
||||||
|
UnsupportedResponseType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum OAuth2TokenType {
|
||||||
|
Bearer,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2AuthorizeRequest {
|
||||||
|
pub response_type: OAuth2ResponseType,
|
||||||
|
pub client_id: String,
|
||||||
|
pub client_secret: Option<String>,
|
||||||
|
pub redirect_uri: String,
|
||||||
|
pub scope: Option<String>,
|
||||||
|
pub state: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2CodeResponse {
|
||||||
|
pub code: String,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub state: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2ErrorResponse {
|
||||||
|
pub error: OAuth2AuthorizeError,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub error_description: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub error_uri: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub state: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2TokenRequest {
|
||||||
|
pub grant_type: OAuth2GrantType,
|
||||||
|
pub client_id: Option<String>,
|
||||||
|
pub client_secret: Option<String>,
|
||||||
|
pub redirect_uri: Option<String>,
|
||||||
|
pub code: Option<String>,
|
||||||
|
pub refresh_token: Option<String>,
|
||||||
|
pub username: Option<String>,
|
||||||
|
pub password: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2TokenResponse {
|
||||||
|
pub token_type: OAuth2TokenType,
|
||||||
|
|
||||||
|
pub access_token: String,
|
||||||
|
|
||||||
|
pub refresh_token: String,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub scope: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub state: Option<String>,
|
||||||
|
|
||||||
|
pub expires_in: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2RevokeRequest {
|
||||||
|
pub token: String,
|
||||||
|
pub token_type_hint: OAuth2TokenHintType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct OAuth2IntrospectionResponse {
|
||||||
|
pub active: bool,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub client_id: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub username: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub scope: Option<String>,
|
||||||
|
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub exp: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OAuth2IntrospectionResponse {
|
||||||
|
fn inative() -> Self {
|
||||||
|
Self {
|
||||||
|
active: false,
|
||||||
|
client_id: None,
|
||||||
|
username: None,
|
||||||
|
scope: None,
|
||||||
|
exp: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/token.rs
Normal file
20
src/token.rs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum TokenType {
|
||||||
|
User,
|
||||||
|
Bot,
|
||||||
|
Client,
|
||||||
|
Server,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum AuthorizationType {
|
||||||
|
ApiKey,
|
||||||
|
Basic,
|
||||||
|
Bearer,
|
||||||
|
}
|
Reference in New Issue
Block a user