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:
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:
= "YOUR_CLIENT_ID"
= "YOUR_CLIENT_SECRET"
= "YOUR_TENANT_ID"
= "https://graph.microsoft.com/.default"
To obtain these values:
- Go to https://portal.azure.com
- Navigate to Azure Active Directory > App Registrations
- Register a new app
- Under Overview, copy the
Application (client) IDandDirectory (tenant) ID. - Under Certificates & secrets, create a new client secret.
- Under API Permissions, add
Microsoft Graph > Delegated | User.Reador 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:
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)
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
- Rust (use rustup to install)
- A registered Azure AD app with the following:
- client_id
- tenant_id
- client_secret
- Scopes (e.g., https://graph.microsoft.com/.default)
License
This project is licensed under the MIT License. See the LICENSE file for more information.