1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! Target-agnostic PKCE (RFC 7636) crypto helper for OAuth 2.0 Authorization
//! Code flows.
//!
//! This module provides the pure cryptographic primitives needed to drive an
//! OAuth Authorization Code + PKCE flow — a code verifier, its S256 code
//! challenge, and a CSRF `state` value. Unlike the native CLI flow in
//! [`crate::client::oauth`] (which uses the optional `rand` dependency and is
//! therefore not available on `wasm32`), this module is **ungated** and uses
//! [`getrandom::fill`] for randomness so it compiles and runs identically on
//! the host and on `wasm32-unknown-unknown` (Web Crypto via the `wasm_js`
//! backend).
//!
//! # Why a shared helper
//!
//! Browser PKCE and the existing native loopback flow both need RFC 7636
//! verifier/challenge/state primitives. The native primitives are private and
//! pull in `rand`, which will not build on wasm. This helper extracts the exact
//! same logic (`SHA-256` via the audited [`sha2`] crate, `base64url` no-pad via
//! [`base64`]) with the RNG swapped to `getrandom::fill`, so it is reusable and
//! target-agnostic.
//!
//! # Examples
//!
//! ```
//! use pmcp::shared::pkce::{generate_code_verifier, code_challenge_s256, generate_state};
//!
//! // Generate a fresh PKCE pair for an authorization request.
//! let verifier = generate_code_verifier()?;
//! let challenge = code_challenge_s256(&verifier);
//! let state = generate_state()?;
//!
//! // The verifier is a 43-char base64url (no-pad) string of 32 random bytes.
//! assert_eq!(verifier.len(), 43);
//! // The challenge is deterministic for a given verifier.
//! assert_eq!(challenge, code_challenge_s256(&verifier));
//! # Ok::<(), pmcp::Error>(())
//! ```
//!
//! # RFC 7636 Appendix B vector
//!
//! ```
//! use pmcp::shared::pkce::code_challenge_s256;
//!
//! // The verifier/challenge pair published in RFC 7636 Appendix B.
//! let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
//! let challenge = code_challenge_s256(verifier);
//! assert_eq!(challenge, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM");
//! ```
use crate;
use ;
use ;
/// Number of CSPRNG bytes used to build a code verifier / state value.
///
/// 32 bytes encodes to a 43-character base64url (no-pad) string, which sits
/// inside the RFC 7636 verifier length bounds (43..=128 characters).
const PKCE_RANDOM_BYTES: usize = 32;
/// Fill a fixed-size buffer with cryptographically secure random bytes.
///
/// Centralises the single `getrandom::fill` call so both the verifier and the
/// state generators share one CSPRNG source, and so a `getrandom::Error` is
/// mapped to [`Error::internal`] in exactly one place (no `unwrap`/`expect`).
/// Generate a PKCE code verifier (RFC 7636 §4.1).
///
/// Returns a base64url (no-pad) encoding of 32 cryptographically secure random
/// bytes — a 43-character string drawn from the unreserved character set
/// `[A-Za-z0-9-_]`.
///
/// # Errors
///
/// Returns [`Error::internal`] if the underlying CSPRNG ([`getrandom::fill`])
/// fails to produce randomness (for example, an unsupported target or an OS
/// entropy source error). The function never panics.
///
/// # Examples
///
/// ```
/// use pmcp::shared::pkce::generate_code_verifier;
///
/// let verifier = generate_code_verifier()?;
/// assert_eq!(verifier.len(), 43);
/// assert!(verifier.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_'));
/// # Ok::<(), pmcp::Error>(())
/// ```
/// Compute the S256 PKCE code challenge for a verifier (RFC 7636 §4.2).
///
/// Returns the base64url (no-pad) encoding of `SHA-256(verifier)`. This is
/// deterministic: the same verifier always yields the same challenge, matching
/// the `code_challenge_method=S256` convention validated by the bundled `IdP` in
/// [`crate::server::auth`].
///
/// # Examples
///
/// ```
/// use pmcp::shared::pkce::code_challenge_s256;
///
/// let challenge = code_challenge_s256("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk");
/// assert_eq!(challenge, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM");
/// ```
/// Generate an opaque CSRF `state` value for an authorization request.
///
/// Uses the same CSPRNG source and encoding as [`generate_code_verifier`] (the
/// native flow reuses verifier generation for state), producing a 43-character
/// base64url (no-pad) string suitable as an unguessable, single-use anti-CSRF
/// token bound to the in-flight authorization request.
///
/// # Errors
///
/// Returns [`Error::internal`] if the underlying CSPRNG ([`getrandom::fill`])
/// fails. The function never panics.
///
/// # Examples
///
/// ```
/// use pmcp::shared::pkce::generate_state;
///
/// let state = generate_state()?;
/// assert_eq!(state.len(), 43);
/// # Ok::<(), pmcp::Error>(())
/// ```