Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tweedegolf/axum-github-oauth
Service to handle oauth sessions and provide a github authenticated user to axum servers
https://github.com/tweedegolf/axum-github-oauth
Last synced: about 1 month ago
JSON representation
Service to handle oauth sessions and provide a github authenticated user to axum servers
- Host: GitHub
- URL: https://github.com/tweedegolf/axum-github-oauth
- Owner: tweedegolf
- Created: 2024-07-16T19:51:55.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-26T14:39:09.000Z (5 months ago)
- Last Synced: 2024-07-26T16:26:09.531Z (5 months ago)
- Language: Rust
- Size: 30.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Axum github oauth
Basic github oauth service for axum with an optional check whether a users belongs to a specified organisation.
## Configuration
Environment variables:
```
OAUTH_CLIENT_ID="<...snip...>"
OAUTH_CLIENT_SECRET="<...snip...>"
REDIRECT_URL="https://example.com/authorize"
ORGANISATION="your-organisation-name"
SESSION_KEY="some-long-random-string"
```## Example
```rust
use axum::{
extract::{Request, State},
middleware, RequestExt, Router,
};
use axum_github_oauth::{AuthAction, GithubOauthService, User};/// Middleware to check if the user is authorized.
async fn auth(
State(state): State,
mut request: Request,
) -> Result {
match request.uri().path() {
path if state.is_public(path) => Ok(request),
_ => request
.extract_parts_with_state::(&state)
.await
.map(|_user| request),
}
}#[tokio::main]
async fn main() {
let oauth_service = GithubOauthService::new(None).unwrap();let app = Router::new()
.merge(oauth_service.router())
.layer(middleware::map_request_with_state(
oauth_service.clone(),
auth,
))
.with_state(oauth_service);let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await
.expect("failed to bind TcpListener");axum::serve(listener, app).await.unwrap();
}