|
| 1 | +# sml-oauth |
| 2 | + |
| 3 | +[](https://github.com/sjqtentacles/sml-oauth/actions/workflows/ci.yml) |
| 4 | + |
| 5 | +OAuth 2.0 (RFC 6749) + PKCE (RFC 7636) as a pure sans-IO state machine for |
| 6 | +Standard ML. |
| 7 | + |
| 8 | +Part of the `sjqtentacles` monorepo of SML libraries. It builds on |
| 9 | +[`sml-codec`](https://github.com/sjqtentacles/sml-codec) (Base64 + SHA-256 for |
| 10 | +PKCE), [`sml-uri`](https://github.com/sjqtentacles/sml-uri) (URL building), |
| 11 | +[`sml-json`](https://github.com/sjqtentacles/sml-json) (token response |
| 12 | +parsing), and [`sml-random`](https://github.com/sjqtentacles/sml-random) |
| 13 | +(seeded verifier generation). |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- `OAuthPkce` — generate `{verifier, challenge, method}` from a deterministic |
| 18 | + seed. Challenge = `Base64.encodeUrl (Sha256.digest verifier)` per RFC 7636 |
| 19 | + section 4.2. Verified against the RFC 7636 Appendix B test vector. |
| 20 | +- `OAuthUrl` — authorization-endpoint URL builder with all required params |
| 21 | + (`response_type=code`, `client_id`, `redirect_uri`, `scope`, `state`, |
| 22 | + `code_challenge`, `code_challenge_method`). |
| 23 | +- `OAuthToken` — token record parsing from RFC 6749 section 5.1 JSON responses. |
| 24 | +- `OAuthClient` — pure FSM: states `Idle | Authorizing | Exchanging | |
| 25 | + Authenticated | Refreshing | Failed`. `step` advances on events |
| 26 | + (`StartAuth`, `CodeReceived`, `TokenReceived`, `TokenExpired`, |
| 27 | + `RefreshReceived`, `Error`). |
| 28 | +- `OAuthRequest` — HTTP request records for the authorization-code and |
| 29 | + refresh-token grant types (form-encoded POST bodies). |
| 30 | + |
| 31 | +## Status |
| 32 | + |
| 33 | +Working — the PKCE test vector, authorization URL params, FSM transitions, |
| 34 | +token JSON parsing, and mismatched-state reset are all covered by the test |
| 35 | +suite. |
| 36 | + |
| 37 | +## Portability |
| 38 | + |
| 39 | +Pure Standard ML using only the Basis library (plus the vendored `sml-codec`, |
| 40 | +`sml-uri`, `sml-json`, and `sml-random`) — no FFI, no threads, no sockets. |
| 41 | +Verified on **MLton** and **Poly/ML**, with identical, deterministic output |
| 42 | +across both. |
| 43 | + |
| 44 | +## Building and testing |
| 45 | + |
| 46 | +```sh |
| 47 | +make test # build + run the suite under MLton (default) |
| 48 | +make test-poly # run the suite under Poly/ML |
| 49 | +make all-tests # run under both |
| 50 | +make clean |
| 51 | +``` |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +```sml |
| 56 | +(* Generate a PKCE pair from a seed. *) |
| 57 | +val rng = Random.fromInt 42 |
| 58 | +val (pkce, rng') = OAuth.OAuthPkce.generate rng |
| 59 | +
|
| 60 | +(* Build the authorization URL. *) |
| 61 | +val url = OAuth.OAuthUrl.authorizationUrl |
| 62 | + { endpoint = "https://auth.example.com/authorize", |
| 63 | + clientId = "client-123", |
| 64 | + redirectUri = "https://app.example.com/callback", |
| 65 | + scopes = ["read", "write"], |
| 66 | + state = "xyz", |
| 67 | + pkce = pkce } |
| 68 | +
|
| 69 | +(* Drive the FSM. *) |
| 70 | +val session = |
| 71 | + { clientId = "client-123", |
| 72 | + redirectUri = "https://app.example.com/callback", |
| 73 | + tokenEndpoint = "https://auth.example.com/token", |
| 74 | + clientSecret = NONE } |
| 75 | +val (state1, _) = OAuth.OAuthClient.step session |
| 76 | + OAuth.OAuthClient.Idle |
| 77 | + (OAuth.OAuthClient.StartAuth (pkce, "xyz")) |
| 78 | +(* ... redirect the user; receive callback with code + state ... *) |
| 79 | +val (state2, _) = OAuth.OAuthClient.step session state1 |
| 80 | + (OAuth.OAuthClient.CodeReceived ("thecode", "xyz")) |
| 81 | +(* ... execute the token exchange request; parse JSON ... *) |
| 82 | +val (state3, _) = OAuth.OAuthClient.step session state2 |
| 83 | + (OAuth.OAuthClient.TokenReceived tokenJson) |
| 84 | +``` |
| 85 | + |
| 86 | +## PKCE test vector |
| 87 | + |
| 88 | +The library is verified against [RFC 7636 Appendix B](https://datatracker.ietf.org/doc/html/rfc7636#appendix-B): |
| 89 | +verifier `"dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"` produces challenge |
| 90 | +`"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"`. |
| 91 | + |
| 92 | +## Dependencies |
| 93 | + |
| 94 | +- [`sml-codec`](https://github.com/sjqtentacles/sml-codec) — `Base64.encodeUrl` |
| 95 | + and `Sha256.digest` for PKCE. |
| 96 | +- [`sml-uri`](https://github.com/sjqtentacles/sml-uri) — `Percent.encodeForm` |
| 97 | + for query string construction. |
| 98 | +- [`sml-json`](https://github.com/sjqtentacles/sml-json) — token response |
| 99 | + parsing (transitively vendors `sml-parsec`). |
| 100 | +- [`sml-random`](https://github.com/sjqtentacles/sml-random) — SplitMix64-based |
| 101 | + seeded verifier generation. |
| 102 | + |
| 103 | +`sml-crypto` is intentionally NOT a dependency — PKCE only needs SHA-256 and |
| 104 | +Base64URL, both provided by `sml-codec`. |
| 105 | + |
| 106 | +## Installing with smlpkg |
| 107 | + |
| 108 | +```sh |
| 109 | +smlpkg add github.com/sjqtentacles/sml-oauth |
| 110 | +smlpkg sync |
| 111 | +``` |
| 112 | + |
| 113 | +Then reference the library basis from your own `.mlb`: |
| 114 | + |
| 115 | +``` |
| 116 | +lib/github.com/sjqtentacles/sml-oauth/sml-oauth.mlb |
| 117 | +``` |
| 118 | + |
| 119 | +## License |
| 120 | + |
| 121 | +MIT. See [LICENSE](LICENSE). |
0 commit comments