msoauth

Crates.iomsoauth
lib.rsmsoauth
version1.1.1
created_at2025-05-23 12:00:28.648526+00
updated_at2025-06-29 20:55:28.162227+00
descriptionA simple Microsoft OAuth2 client
homepage
repository
max_upload_size
id1686222
size68,186
Shane Poppleton (codemonkey76)

documentation

README

MSOAuth

MSOAuth is a simple command-line tool for obtaining and refreshing Microsoft OAuth2 tokens.

Installation

To install the MSOAuth tool, you'll need to have Rust and Cargo installed on your system. Once you have them, you can build and install the project using:

cargo install --path .

This will create an executable in the target/release directory.

Configuration

Create a configuration file config.toml located in the msoauth directory within your system's config directory, e.g., ~/.config/msoauth/config.toml. The configuration file should look like this:

client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
tenant_id = "YOUR_TENANT_ID"
scope = "https://graph.microsoft.com/.default"

To obtain these values:

  1. Go to https://portal.azure.com
  2. Navigate to Azure Active Directory > App Registrations
  3. Register a new app
  4. Under Overview, copy the Application (client) ID and Directory (tenant) ID.
  5. Under Certificates & secrets, create a new client secret.
  6. Under API Permissions, add Microsoft Graph > Delegated | User.Read or other needed scopes.

Usage

The MSOAuth tool provides several command-line options:

  • --print-token: Prints the current access token if it's valid. If the token is expired or close to expiring, it will attempt to refresh it.
  • --refresh: Forces a token refresh.
  • --login: Initiates a device login flow to obtain a new token.
  • --clear-token: Deletes the saved token file.

Example usage:

msoauth --login         # Start device login flow
msoauth --refresh       # Refresh the token if expired
msoauth --print-token   # Print current access token (refresh if needed)
msoauth --clear-token   # Delete the saved token file
msoauth                 # Default, try refresh, fallback to login

Integration Example (mbsync)

You can use MSOAuth in your mbsyncrc file with:

PassCmd "msoauth --print-token"

This will ensure that only the token is printed and no other messages interfere with the mbsync operations.

Integration Example (NeoMutt)

set imap_pass="`msoauth --print-token`"

Features

  • Authenticates using Microsoft OAuth2 Device Code flow
  • Automatically saves/refreshes access tokens
  • Prints access token for use in scripts or email clients
  • Logs activity via tracing
  • Friendly error messages and self-healing default mode

Requirements

License

This project is licensed under the MIT License. See the LICENSE file for more information.

Commit count: 0

cargo fmt