···421421 Log.err (fun m -> m "Failed to apply HTTP message signature: %s"
422422 (Signature.sign_error_to_string e));
423423 headers)
424424- | _ -> headers424424+ | _ -> headers
425425+
+6-2
lib/auth.mli
···1010 - {b Basic}: {{:https://datatracker.ietf.org/doc/html/rfc7617}RFC 7617} - Base64 username:password
1111 - {b Bearer}: {{:https://datatracker.ietf.org/doc/html/rfc6750}RFC 6750} - OAuth 2.0 tokens
1212 - {b Digest}: {{:https://datatracker.ietf.org/doc/html/rfc7616}RFC 7616} - Challenge-response with MD5/SHA-256
1313+ - {b Signature}: {{:https://datatracker.ietf.org/doc/html/rfc9421}RFC 9421} - HTTP Message Signatures
1414+1515+ For OAuth 2.0 with automatic token refresh, see the [requests.oauth] subpackage.
13161417 {2 Security}
1518···5558 The signature covers selected headers and derived values like
5659 the method, path, and authority.
57605858- Use {!Signature.config} to create the configuration:
6161+ Use {!val:Signature.config} to create the configuration:
5962 {[
6063 let key = Signature.Key.ed25519 ~priv:... ~pub:... in
6164 let config = Signature.config ~key ~keyid:"my-key" () in
···212215 the appropriate headers per RFC 9421.
213216214217 If [auth] is not Signature authentication, returns [headers] unchanged.
215215- If signature computation fails, logs an error and returns [headers] unchanged. *)218218+ If signature computation fails, logs an error and returns [headers] unchanged. *)
219219+
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** RFC 6749 OAuth 2.0 Authorization Framework. *)
77+88+let src = Logs.Src.create "requests.oauth" ~doc:"OAuth 2.0"
99+module Log = (val Logs.src_log src : Logs.LOG)
1010+1111+(** {1 Client Configuration} *)
1212+1313+type config = {
1414+ client_id : string;
1515+ client_secret : string option;
1616+ token_endpoint : string;
1717+ authorization_endpoint : string option;
1818+ redirect_uri : string option;
1919+ scopes : string list;
2020+}
2121+2222+let make_config ~client_id ?client_secret ~token_endpoint
2323+ ?authorization_endpoint ?redirect_uri ?(scopes = []) () =
2424+ { client_id; client_secret; token_endpoint;
2525+ authorization_endpoint; redirect_uri; scopes }
2626+2727+(** {1 Token Types} *)
2828+2929+type token = {
3030+ access_token : string;
3131+ token_type : string;
3232+ expires_at : Ptime.t option;
3333+ refresh_token : string option;
3434+ scope : string option;
3535+}
3636+3737+let get_access_token t = t.access_token
3838+let get_refresh_token t = t.refresh_token
3939+4040+let now () = Ptime_clock.now ()
4141+4242+let is_expired token =
4343+ match token.expires_at with
4444+ | None -> false
4545+ | Some expires_at -> not (Ptime.is_later expires_at ~than:(now ()))
4646+4747+let expires_within span token =
4848+ match token.expires_at with
4949+ | None -> false
5050+ | Some expires_at ->
5151+ match Ptime.sub_span expires_at span with
5252+ | None -> true (* Overflow, assume expiring *)
5353+ | Some threshold -> not (Ptime.is_later threshold ~than:(now ()))
5454+5555+(** {1 Error Types} *)
5656+5757+type error_code =
5858+ | Invalid_request
5959+ | Invalid_client
6060+ | Invalid_grant
6161+ | Unauthorized_client
6262+ | Unsupported_grant_type
6363+ | Invalid_scope
6464+ | Unknown_error of string
6565+6666+type error = {
6767+ code : error_code;
6868+ description : string option;
6969+ uri : string option;
7070+}
7171+7272+let error_code_of_string = function
7373+ | "invalid_request" -> Invalid_request
7474+ | "invalid_client" -> Invalid_client
7575+ | "invalid_grant" -> Invalid_grant
7676+ | "unauthorized_client" -> Unauthorized_client
7777+ | "unsupported_grant_type" -> Unsupported_grant_type
7878+ | "invalid_scope" -> Invalid_scope
7979+ | s -> Unknown_error s
8080+8181+let error_code_to_string = function
8282+ | Invalid_request -> "invalid_request"
8383+ | Invalid_client -> "invalid_client"
8484+ | Invalid_grant -> "invalid_grant"
8585+ | Unauthorized_client -> "unauthorized_client"
8686+ | Unsupported_grant_type -> "unsupported_grant_type"
8787+ | Invalid_scope -> "invalid_scope"
8888+ | Unknown_error s -> s
8989+9090+let pp_error ppf err =
9191+ Format.fprintf ppf "OAuth error: %s" (error_code_to_string err.code);
9292+ Option.iter (fun desc -> Format.fprintf ppf " - %s" desc) err.description;
9393+ Option.iter (fun uri -> Format.fprintf ppf " (see: %s)" uri) err.uri
9494+9595+(** {1 PKCE Support} *)
9696+9797+type pkce_method = Plain | S256
9898+9999+type pkce = {
100100+ verifier : string;
101101+ challenge : string;
102102+ method_ : pkce_method;
103103+}
104104+105105+let pkce_method_to_string = function
106106+ | Plain -> "plain"
107107+ | S256 -> "S256"
108108+109109+(** URL-safe base64 encoding without padding per RFC 7636 Appendix A *)
110110+let base64_url_encode_no_padding s =
111111+ Base64.encode_exn s
112112+ |> String.map (function '+' -> '-' | '/' -> '_' | c -> c)
113113+ |> String.to_seq
114114+ |> Seq.filter (fun c -> c <> '=')
115115+ |> String.of_seq
116116+117117+let generate_verifier () =
118118+ Mirage_crypto_rng.generate 32 |> base64_url_encode_no_padding
119119+120120+let compute_challenge ~method_ verifier =
121121+ match method_ with
122122+ | Plain -> verifier
123123+ | S256 ->
124124+ Digestif.SHA256.digest_string verifier
125125+ |> Digestif.SHA256.to_raw_string
126126+ |> base64_url_encode_no_padding
127127+128128+let generate_pkce ?(method_ = S256) () =
129129+ let verifier = generate_verifier () in
130130+ let challenge = compute_challenge ~method_ verifier in
131131+ { verifier; challenge; method_ }
132132+133133+(** {1 State Parameter} *)
134134+135135+let generate_state () =
136136+ Mirage_crypto_rng.generate 16 |> base64_url_encode_no_padding
137137+138138+let validate_state ~expected ~received =
139139+ Eqaf.equal expected received
140140+141141+(** {1 Authorization URL} *)
142142+143143+let add_opt_param key opt params =
144144+ Option.fold ~none:params ~some:(fun v -> (key, v) :: params) opt
145145+146146+let authorization_url ~config ~state ?pkce ?(extra_params = []) () =
147147+ match config.authorization_endpoint with
148148+ | None ->
149149+ invalid_arg "authorization_endpoint is required for authorization URL"
150150+ | Some endpoint ->
151151+ let params =
152152+ [ ("response_type", "code");
153153+ ("client_id", config.client_id);
154154+ ("state", state) ]
155155+ |> add_opt_param "redirect_uri" config.redirect_uri
156156+ |> (fun params ->
157157+ match config.scopes with
158158+ | [] -> params
159159+ | scopes -> ("scope", String.concat " " scopes) :: params)
160160+ |> (fun params ->
161161+ match pkce with
162162+ | None -> params
163163+ | Some p ->
164164+ ("code_challenge", p.challenge)
165165+ :: ("code_challenge_method", pkce_method_to_string p.method_)
166166+ :: params)
167167+ |> List.rev_append extra_params
168168+ in
169169+ let uri = Uri.of_string endpoint in
170170+ let params_list = List.map (fun (k, v) -> (k, [v])) params in
171171+ Uri.to_string (Uri.add_query_params uri params_list)
172172+173173+(** {1 JSON Codecs} *)
174174+175175+let string_option = Jsont.(some string)
176176+let int_option = Jsont.(some int)
177177+178178+(** Token response JSON codec *)
179179+let token_response_jsont =
180180+ let make access_token token_type expires_in refresh_token scope =
181181+ let received_at = now () in
182182+ let expires_at =
183183+ Option.bind expires_in (fun secs ->
184184+ Ptime.add_span received_at (Ptime.Span.of_int_s secs))
185185+ in
186186+ { access_token; token_type; expires_at; refresh_token; scope }
187187+ in
188188+ Jsont.Object.map ~kind:"token_response" make
189189+ |> Jsont.Object.mem "access_token" Jsont.string ~enc:(fun t -> t.access_token)
190190+ |> Jsont.Object.mem "token_type" Jsont.string ~enc:(fun t -> t.token_type)
191191+ |> Jsont.Object.mem "expires_in" int_option ~dec_absent:None
192192+ ~enc:(fun _ -> None) (* Don't encode expires_in, it's derived *)
193193+ |> Jsont.Object.mem "refresh_token" string_option ~dec_absent:None
194194+ ~enc:(fun t -> t.refresh_token)
195195+ |> Jsont.Object.mem "scope" string_option ~dec_absent:None
196196+ ~enc:(fun t -> t.scope)
197197+ |> Jsont.Object.finish
198198+199199+let error_code_jsont =
200200+ Jsont.string
201201+ |> Jsont.map ~dec:error_code_of_string ~enc:error_code_to_string
202202+203203+let error_jsont =
204204+ let make code description uri = { code; description; uri } in
205205+ Jsont.Object.map ~kind:"oauth_error" make
206206+ |> Jsont.Object.mem "error" error_code_jsont ~enc:(fun e -> e.code)
207207+ |> Jsont.Object.mem "error_description" string_option ~dec_absent:None
208208+ ~enc:(fun e -> e.description)
209209+ |> Jsont.Object.mem "error_uri" string_option ~dec_absent:None
210210+ ~enc:(fun e -> e.uri)
211211+ |> Jsont.Object.finish
212212+213213+(** {1 Token Operations} *)
214214+215215+let parse_token_response ~status ~body =
216216+ Log.debug (fun m -> m "Token response status=%d" status);
217217+ if status >= 200 && status < 300 then
218218+ match Jsont_bytesrw.decode_string' token_response_jsont body with
219219+ | Ok token ->
220220+ Log.info (fun m -> m "Received access token (type=%s)" token.token_type);
221221+ Ok token
222222+ | Error e ->
223223+ Log.err (fun m -> m "Failed to parse token response: %s"
224224+ (Jsont.Error.to_string e));
225225+ Error {
226226+ code = Invalid_request;
227227+ description = Some ("Failed to parse token response: " ^
228228+ Jsont.Error.to_string e);
229229+ uri = None;
230230+ }
231231+ else
232232+ match Jsont_bytesrw.decode_string' error_jsont body with
233233+ | Ok err ->
234234+ Log.warn (fun m -> m "OAuth error: %a" pp_error err);
235235+ Error err
236236+ | Error e ->
237237+ Log.err (fun m -> m "Failed to parse error response: %s (status=%d)"
238238+ (Jsont.Error.to_string e) status);
239239+ Error {
240240+ code = Unknown_error "parse_error";
241241+ description = Some (Printf.sprintf "HTTP %d: %s" status body);
242242+ uri = None;
243243+ }
244244+245245+let add_client_auth config headers body_params =
246246+ match config.client_secret with
247247+ | Some secret ->
248248+ let headers = Requests.Headers.basic
249249+ ~username:config.client_id
250250+ ~password:secret
251251+ headers
252252+ in
253253+ (headers, body_params)
254254+ | None ->
255255+ let body_params = ("client_id", config.client_id) :: body_params in
256256+ (headers, body_params)
257257+258258+let token_request session config ~grant_type ~params =
259259+ let headers = Requests.Headers.empty in
260260+ let body_params = ("grant_type", grant_type) :: params in
261261+ let body_params =
262262+ match config.scopes with
263263+ | [] -> body_params
264264+ | scopes -> ("scope", String.concat " " scopes) :: body_params
265265+ in
266266+ let headers, body_params = add_client_auth config headers body_params in
267267+ Log.debug (fun m -> m "Token request to %s: grant_type=%s"
268268+ config.token_endpoint grant_type);
269269+ let body = Requests.Body.form body_params in
270270+ let response = Requests.post session ~headers ~body config.token_endpoint in
271271+ let status = Requests.Response.status_code response in
272272+ let body = Requests.Response.text response in
273273+ parse_token_response ~status ~body
274274+275275+let client_credentials session config =
276276+ Log.info (fun m -> m "Performing client credentials grant");
277277+ token_request session config ~grant_type:"client_credentials" ~params:[]
278278+279279+let password_grant session config ~username ~password =
280280+ Log.info (fun m -> m "Performing password credentials grant for user: %s" username);
281281+ let params = [("username", username); ("password", password)] in
282282+ token_request session config ~grant_type:"password" ~params
283283+284284+let exchange_code session config ~code ?pkce_verifier () =
285285+ Log.info (fun m -> m "Exchanging authorization code for tokens");
286286+ let params =
287287+ [("code", code)]
288288+ |> add_opt_param "redirect_uri" config.redirect_uri
289289+ |> add_opt_param "code_verifier" pkce_verifier
290290+ in
291291+ token_request session config ~grant_type:"authorization_code" ~params
292292+293293+let refresh session config ~refresh_token =
294294+ Log.info (fun m -> m "Refreshing access token");
295295+ let params = [("refresh_token", refresh_token)] in
296296+ token_request session config ~grant_type:"refresh_token" ~params
297297+298298+(** {1 Managed Token State} *)
299299+300300+type t = {
301301+ session : Requests.t;
302302+ config : config;
303303+ mutable token : token;
304304+ mutex : Eio.Mutex.t;
305305+ on_refresh : (token -> unit) option;
306306+}
307307+308308+let default_leeway = Ptime.Span.of_int_s 30
309309+310310+let create session config token ?on_refresh () =
311311+ { session; config; token; mutex = Eio.Mutex.create (); on_refresh }
312312+313313+let force_refresh t =
314314+ Eio.Mutex.use_rw ~protect:true t.mutex (fun () ->
315315+ match t.token.refresh_token with
316316+ | None ->
317317+ Log.warn (fun m -> m "Cannot refresh: no refresh token available");
318318+ Error {
319319+ code = Invalid_grant;
320320+ description = Some "No refresh token available";
321321+ uri = None;
322322+ }
323323+ | Some refresh_token ->
324324+ match refresh t.session t.config ~refresh_token with
325325+ | Ok new_token ->
326326+ (* Preserve refresh token if new response doesn't include one *)
327327+ let new_token =
328328+ if new_token.refresh_token = None then
329329+ { new_token with refresh_token = Some refresh_token }
330330+ else
331331+ new_token
332332+ in
333333+ t.token <- new_token;
334334+ Option.iter (fun f -> f new_token) t.on_refresh;
335335+ Ok new_token
336336+ | Error e ->
337337+ Log.err (fun m -> m "Token refresh failed: %a" pp_error e);
338338+ Error e)
339339+340340+let check_and_refresh t =
341341+ if expires_within default_leeway t.token then begin
342342+ Log.debug (fun m -> m "Token needs refresh, refreshing...");
343343+ match force_refresh t with
344344+ | Ok _ -> ()
345345+ | Error e -> Log.warn (fun m -> m "Auto-refresh failed: %a" pp_error e)
346346+ end
347347+348348+let get_token t =
349349+ check_and_refresh t;
350350+ t.token
351351+352352+let get_access_token_managed t =
353353+ (get_token t).access_token
354354+355355+let with_client_credentials session config ?on_refresh () =
356356+ client_credentials session config
357357+ |> Result.map (fun token -> create session config token ?on_refresh ())
+284
lib/oauth/oauth.mli
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** RFC 6749 OAuth 2.0 Authorization Framework.
77+88+ This module implements the OAuth 2.0 authorization framework as specified in
99+ {{:https://datatracker.ietf.org/doc/html/rfc6749}RFC 6749}.
1010+1111+ {2 Supported Grant Types}
1212+ - {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.1}Authorization Code} (Section 4.1)
1313+ - {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.3}Resource Owner Password Credentials} (Section 4.3)
1414+ - {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.4}Client Credentials} (Section 4.4)
1515+1616+ The Implicit Grant (Section 4.2) is intentionally not supported as it is
1717+ deprecated per {{:https://datatracker.ietf.org/doc/html/rfc8996}RFC 8996}.
1818+1919+ {2 PKCE Support}
2020+2121+ This module supports Proof Key for Code Exchange (PKCE) per
2222+ {{:https://datatracker.ietf.org/doc/html/rfc7636}RFC 7636} to protect
2323+ against authorization code interception attacks, especially for public clients.
2424+2525+ {2 Usage Example}
2626+2727+ {[
2828+ (* Client credentials grant *)
2929+ let config = Oauth.make_config
3030+ ~client_id:"my-client"
3131+ ~client_secret:"my-secret"
3232+ ~token_endpoint:"https://auth.example.com/token"
3333+ () in
3434+ match Oauth.client_credentials session config with
3535+ | Ok token -> Printf.printf "Got token: %s\n" (Oauth.get_access_token token)
3636+ | Error e -> Printf.printf "Error: %a\n" Oauth.pp_error e
3737+3838+ (* Authorization code flow with PKCE *)
3939+ let pkce = Oauth.generate_pkce () in
4040+ let state = Oauth.generate_state () in
4141+ let auth_url = Oauth.authorization_url ~config ~state ~pkce () in
4242+ (* ... redirect user to auth_url, receive code ... *)
4343+ match Oauth.exchange_code session config ~code ~pkce_verifier:pkce.verifier () with
4444+ | Ok token -> ...
4545+ | Error e -> ...
4646+ ]}
4747+4848+ {2 References}
4949+ {ul
5050+ {- {{:https://datatracker.ietf.org/doc/html/rfc6749}RFC 6749} - OAuth 2.0 Authorization Framework}
5151+ {- {{:https://datatracker.ietf.org/doc/html/rfc7636}RFC 7636} - PKCE (Proof Key for Code Exchange)}
5252+ {- {{:https://datatracker.ietf.org/doc/html/rfc6750}RFC 6750} - Bearer Token Usage}
5353+ {- {{:https://datatracker.ietf.org/doc/html/rfc8996}RFC 8996} - Deprecates Implicit Grant}} *)
5454+5555+(** {1 Client Configuration} *)
5656+5757+(** OAuth 2.0 client configuration.
5858+5959+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-2}RFC 6749 Section 2},
6060+ clients are identified by a client ID and optionally authenticated with a client secret. *)
6161+type config = {
6262+ client_id : string;
6363+ (** The client identifier issued during registration.
6464+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-2.2}Section 2.2}. *)
6565+6666+ client_secret : string option;
6767+ (** The client secret for confidential clients. [None] for public clients.
6868+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-2.3.1}Section 2.3.1}. *)
6969+7070+ token_endpoint : string;
7171+ (** The authorization server's token endpoint URL.
7272+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-3.2}Section 3.2}. *)
7373+7474+ authorization_endpoint : string option;
7575+ (** The authorization server's authorization endpoint URL.
7676+ Required for Authorization Code grant.
7777+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-3.1}Section 3.1}. *)
7878+7979+ redirect_uri : string option;
8080+ (** The client's redirection endpoint for Authorization Code grant.
8181+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-3.1.2}Section 3.1.2}. *)
8282+8383+ scopes : string list;
8484+ (** The requested access token scope.
8585+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-3.3}Section 3.3}. *)
8686+}
8787+8888+val make_config :
8989+ client_id:string ->
9090+ ?client_secret:string ->
9191+ token_endpoint:string ->
9292+ ?authorization_endpoint:string ->
9393+ ?redirect_uri:string ->
9494+ ?scopes:string list ->
9595+ unit ->
9696+ config
9797+(** [make_config ~client_id ~token_endpoint ...] creates an OAuth client configuration. *)
9898+9999+(** {1 Token Types} *)
100100+101101+(** Token response from the authorization server.
102102+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-5.1}Section 5.1}. *)
103103+type token = {
104104+ access_token : string;
105105+ (** The access token issued by the authorization server. *)
106106+107107+ token_type : string;
108108+ (** The type of the token, typically "Bearer". *)
109109+110110+ expires_at : Ptime.t option;
111111+ (** When the token expires. [None] if no expiry was provided. *)
112112+113113+ refresh_token : string option;
114114+ (** The refresh token for obtaining new access tokens. *)
115115+116116+ scope : string option;
117117+ (** The scope of the access token. *)
118118+}
119119+120120+val get_access_token : token -> string
121121+(** [get_access_token token] returns the access token string. *)
122122+123123+val get_refresh_token : token -> string option
124124+(** [get_refresh_token token] returns the refresh token if present. *)
125125+126126+val is_expired : token -> bool
127127+(** [is_expired token] returns [true] if the token has expired.
128128+ Returns [false] if the token has no expiry information. *)
129129+130130+val expires_within : Ptime.Span.t -> token -> bool
131131+(** [expires_within span token] returns [true] if the token expires within [span].
132132+ Returns [false] if the token has no expiry information. *)
133133+134134+(** {1 Error Types} *)
135135+136136+(** OAuth 2.0 error codes per RFC 6749 Section 5.2. *)
137137+type error_code =
138138+ | Invalid_request
139139+ | Invalid_client
140140+ | Invalid_grant
141141+ | Unauthorized_client
142142+ | Unsupported_grant_type
143143+ | Invalid_scope
144144+ | Unknown_error of string
145145+146146+(** OAuth error response. *)
147147+type error = {
148148+ code : error_code;
149149+ description : string option;
150150+ uri : string option;
151151+}
152152+153153+val pp_error : Format.formatter -> error -> unit
154154+(** Pretty printer for OAuth errors. *)
155155+156156+val error_code_to_string : error_code -> string
157157+(** [error_code_to_string code] returns the RFC 6749 string representation. *)
158158+159159+(** {1 PKCE Support}
160160+161161+ Per {{:https://datatracker.ietf.org/doc/html/rfc7636}RFC 7636}. *)
162162+163163+(** PKCE challenge method. *)
164164+type pkce_method =
165165+ | Plain (** code_challenge = code_verifier (not recommended) *)
166166+ | S256 (** code_challenge = BASE64URL(SHA256(code_verifier)) *)
167167+168168+(** PKCE state for authorization code flow. *)
169169+type pkce = {
170170+ verifier : string;
171171+ (** The code verifier (43-128 URL-safe characters). *)
172172+173173+ challenge : string;
174174+ (** The code challenge derived from the verifier. *)
175175+176176+ method_ : pkce_method;
177177+ (** The challenge derivation method. *)
178178+}
179179+180180+val generate_pkce : ?method_:pkce_method -> unit -> pkce
181181+(** [generate_pkce ()] generates PKCE verifier and challenge.
182182+ Default method is [S256]. *)
183183+184184+val pkce_method_to_string : pkce_method -> string
185185+(** Returns "plain" or "S256". *)
186186+187187+(** {1 State Parameter} *)
188188+189189+val generate_state : unit -> string
190190+(** [generate_state ()] generates a cryptographically random state value
191191+ for CSRF protection per RFC 6749 Section 10.12. *)
192192+193193+val validate_state : expected:string -> received:string -> bool
194194+(** [validate_state ~expected ~received] performs constant-time comparison. *)
195195+196196+(** {1 Authorization URL} *)
197197+198198+val authorization_url :
199199+ config:config ->
200200+ state:string ->
201201+ ?pkce:pkce ->
202202+ ?extra_params:(string * string) list ->
203203+ unit ->
204204+ string
205205+(** [authorization_url ~config ~state ()] builds the authorization URL.
206206+207207+ @raise Invalid_argument if [authorization_endpoint] is not configured. *)
208208+209209+(** {1 Token Operations}
210210+211211+ These functions use a {!Requests.t} session to make HTTP calls. *)
212212+213213+val client_credentials :
214214+ Requests.t ->
215215+ config ->
216216+ (token, error) result
217217+(** [client_credentials session config] performs the client credentials grant.
218218+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.4}Section 4.4}. *)
219219+220220+val password_grant :
221221+ Requests.t ->
222222+ config ->
223223+ username:string ->
224224+ password:string ->
225225+ (token, error) result
226226+(** [password_grant session config ~username ~password] performs the resource owner
227227+ password credentials grant.
228228+229229+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.3}Section 4.3}.
230230+231231+ {b Warning}: This grant type should only be used for legacy or high-trust scenarios. *)
232232+233233+val exchange_code :
234234+ Requests.t ->
235235+ config ->
236236+ code:string ->
237237+ ?pkce_verifier:string ->
238238+ unit ->
239239+ (token, error) result
240240+(** [exchange_code session config ~code ()] exchanges an authorization code for tokens.
241241+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3}Section 4.1.3}. *)
242242+243243+val refresh :
244244+ Requests.t ->
245245+ config ->
246246+ refresh_token:string ->
247247+ (token, error) result
248248+(** [refresh session config ~refresh_token] exchanges a refresh token for a new access token.
249249+ Per {{:https://datatracker.ietf.org/doc/html/rfc6749#section-6}Section 6}. *)
250250+251251+(** {1 Managed Token State}
252252+253253+ Thread-safe automatic token refresh. *)
254254+255255+(** Managed OAuth state with automatic token refresh. *)
256256+type t
257257+258258+val create :
259259+ Requests.t ->
260260+ config ->
261261+ token ->
262262+ ?on_refresh:(token -> unit) ->
263263+ unit ->
264264+ t
265265+(** [create session config token ()] creates managed OAuth state.
266266+ @param on_refresh Optional callback when tokens are refreshed. *)
267267+268268+val get_token : t -> token
269269+(** [get_token t] returns the current token, refreshing if needed. Thread-safe. *)
270270+271271+val get_access_token_managed : t -> string
272272+(** [get_access_token_managed t] returns the current access token, refreshing if needed. *)
273273+274274+val force_refresh : t -> (token, error) result
275275+(** [force_refresh t] forces a token refresh. Thread-safe. *)
276276+277277+val with_client_credentials :
278278+ Requests.t ->
279279+ config ->
280280+ ?on_refresh:(token -> unit) ->
281281+ unit ->
282282+ (t, error) result
283283+(** [with_client_credentials session config ()] performs client credentials grant
284284+ and returns managed state ready for use. *)
+11-8
lib/signature.ml
···410410(* ========================================================================= *)
411411412412module Context = struct
413413+ (** Request context for signature computation. *)
413414 type request_ctx = {
414414- method_ : Method.t;
415415- uri : Uri.t;
416416- headers : Headers.t;
415415+ method_ : Method.t; (** The HTTP method *)
416416+ uri : Uri.t; (** The request URI *)
417417+ headers : Headers.t; (** The request headers *)
417418 }
418419420420+ (** Response context for signature computation. *)
419421 type response_ctx = {
420420- status : int;
421421- headers : Headers.t;
422422- request : request_ctx option;
422422+ status : int; (** The HTTP status code *)
423423+ headers : Headers.t; (** The response headers *)
424424+ request : request_ctx option; (** The original request, if available *)
423425 }
424426427427+ (** Message context (request or response). *)
425428 type t = [
426426- | `Request of request_ctx
427427- | `Response of response_ctx
429429+ | `Request of request_ctx (** A request context *)
430430+ | `Response of response_ctx (** A response context *)
428431 ]
429432430433 let request ~method_ ~uri ~headers : t =
+8-6
lib/signature.mli
···277277 (** Context for resolving message components. *)
278278279279 type request_ctx = {
280280- method_ : Method.t;
281281- uri : Uri.t;
282282- headers : Headers.t;
280280+ method_ : Method.t; (** The HTTP method *)
281281+ uri : Uri.t; (** The request URI *)
282282+ headers : Headers.t; (** The request headers *)
283283 }
284284+ (** Request context for signature computation. *)
284285285286 type response_ctx = {
286286- status : int;
287287- headers : Headers.t;
288288- request : request_ctx option;
287287+ status : int; (** The HTTP status code *)
288288+ headers : Headers.t; (** The response headers *)
289289+ request : request_ctx option; (** The original request, if available *)
289290 }
291291+ (** Response context for signature computation. *)
290292291293 type t = [
292294 | `Request of request_ctx
+4259
spec/rfc6749.txt
···11+22+33+44+55+66+77+Internet Engineering Task Force (IETF) D. Hardt, Ed.
88+Request for Comments: 6749 Microsoft
99+Obsoletes: 5849 October 2012
1010+Category: Standards Track
1111+ISSN: 2070-1721
1212+1313+1414+ The OAuth 2.0 Authorization Framework
1515+1616+Abstract
1717+1818+ The OAuth 2.0 authorization framework enables a third-party
1919+ application to obtain limited access to an HTTP service, either on
2020+ behalf of a resource owner by orchestrating an approval interaction
2121+ between the resource owner and the HTTP service, or by allowing the
2222+ third-party application to obtain access on its own behalf. This
2323+ specification replaces and obsoletes the OAuth 1.0 protocol described
2424+ in RFC 5849.
2525+2626+Status of This Memo
2727+2828+ This is an Internet Standards Track document.
2929+3030+ This document is a product of the Internet Engineering Task Force
3131+ (IETF). It represents the consensus of the IETF community. It has
3232+ received public review and has been approved for publication by the
3333+ Internet Engineering Steering Group (IESG). Further information on
3434+ Internet Standards is available in Section 2 of RFC 5741.
3535+3636+ Information about the current status of this document, any errata,
3737+ and how to provide feedback on it may be obtained at
3838+ http://www.rfc-editor.org/info/rfc6749.
3939+4040+Copyright Notice
4141+4242+ Copyright (c) 2012 IETF Trust and the persons identified as the
4343+ document authors. All rights reserved.
4444+4545+ This document is subject to BCP 78 and the IETF Trust's Legal
4646+ Provisions Relating to IETF Documents
4747+ (http://trustee.ietf.org/license-info) in effect on the date of
4848+ publication of this document. Please review these documents
4949+ carefully, as they describe your rights and restrictions with respect
5050+ to this document. Code Components extracted from this document must
5151+ include Simplified BSD License text as described in Section 4.e of
5252+ the Trust Legal Provisions and are provided without warranty as
5353+ described in the Simplified BSD License.
5454+5555+5656+5757+5858+Hardt Standards Track [Page 1]
5959+6060+RFC 6749 OAuth 2.0 October 2012
6161+6262+6363+Table of Contents
6464+6565+ 1. Introduction ....................................................4
6666+ 1.1. Roles ......................................................6
6767+ 1.2. Protocol Flow ..............................................7
6868+ 1.3. Authorization Grant ........................................8
6969+ 1.3.1. Authorization Code ..................................8
7070+ 1.3.2. Implicit ............................................8
7171+ 1.3.3. Resource Owner Password Credentials .................9
7272+ 1.3.4. Client Credentials ..................................9
7373+ 1.4. Access Token ..............................................10
7474+ 1.5. Refresh Token .............................................10
7575+ 1.6. TLS Version ...............................................12
7676+ 1.7. HTTP Redirections .........................................12
7777+ 1.8. Interoperability ..........................................12
7878+ 1.9. Notational Conventions ....................................13
7979+ 2. Client Registration ............................................13
8080+ 2.1. Client Types ..............................................14
8181+ 2.2. Client Identifier .........................................15
8282+ 2.3. Client Authentication .....................................16
8383+ 2.3.1. Client Password ....................................16
8484+ 2.3.2. Other Authentication Methods .......................17
8585+ 2.4. Unregistered Clients ......................................17
8686+ 3. Protocol Endpoints .............................................18
8787+ 3.1. Authorization Endpoint ....................................18
8888+ 3.1.1. Response Type ......................................19
8989+ 3.1.2. Redirection Endpoint ...............................19
9090+ 3.2. Token Endpoint ............................................21
9191+ 3.2.1. Client Authentication ..............................22
9292+ 3.3. Access Token Scope ........................................23
9393+ 4. Obtaining Authorization ........................................23
9494+ 4.1. Authorization Code Grant ..................................24
9595+ 4.1.1. Authorization Request ..............................25
9696+ 4.1.2. Authorization Response .............................26
9797+ 4.1.3. Access Token Request ...............................29
9898+ 4.1.4. Access Token Response ..............................30
9999+ 4.2. Implicit Grant ............................................31
100100+ 4.2.1. Authorization Request ..............................33
101101+ 4.2.2. Access Token Response ..............................35
102102+ 4.3. Resource Owner Password Credentials Grant .................37
103103+ 4.3.1. Authorization Request and Response .................39
104104+ 4.3.2. Access Token Request ...............................39
105105+ 4.3.3. Access Token Response ..............................40
106106+ 4.4. Client Credentials Grant ..................................40
107107+ 4.4.1. Authorization Request and Response .................41
108108+ 4.4.2. Access Token Request ...............................41
109109+ 4.4.3. Access Token Response ..............................42
110110+ 4.5. Extension Grants ..........................................42
111111+112112+113113+114114+Hardt Standards Track [Page 2]
115115+116116+RFC 6749 OAuth 2.0 October 2012
117117+118118+119119+ 5. Issuing an Access Token ........................................43
120120+ 5.1. Successful Response .......................................43
121121+ 5.2. Error Response ............................................45
122122+ 6. Refreshing an Access Token .....................................47
123123+ 7. Accessing Protected Resources ..................................48
124124+ 7.1. Access Token Types ........................................49
125125+ 7.2. Error Response ............................................49
126126+ 8. Extensibility ..................................................50
127127+ 8.1. Defining Access Token Types ...............................50
128128+ 8.2. Defining New Endpoint Parameters ..........................50
129129+ 8.3. Defining New Authorization Grant Types ....................51
130130+ 8.4. Defining New Authorization Endpoint Response Types ........51
131131+ 8.5. Defining Additional Error Codes ...........................51
132132+ 9. Native Applications ............................................52
133133+ 10. Security Considerations .......................................53
134134+ 10.1. Client Authentication ....................................53
135135+ 10.2. Client Impersonation .....................................54
136136+ 10.3. Access Tokens ............................................55
137137+ 10.4. Refresh Tokens ...........................................55
138138+ 10.5. Authorization Codes ......................................56
139139+ 10.6. Authorization Code Redirection URI Manipulation ..........56
140140+ 10.7. Resource Owner Password Credentials ......................57
141141+ 10.8. Request Confidentiality ..................................58
142142+ 10.9. Ensuring Endpoint Authenticity ...........................58
143143+ 10.10. Credentials-Guessing Attacks ............................58
144144+ 10.11. Phishing Attacks ........................................58
145145+ 10.12. Cross-Site Request Forgery ..............................59
146146+ 10.13. Clickjacking ............................................60
147147+ 10.14. Code Injection and Input Validation .....................60
148148+ 10.15. Open Redirectors ........................................60
149149+ 10.16. Misuse of Access Token to Impersonate Resource
150150+ Owner in Implicit Flow ..................................61
151151+ 11. IANA Considerations ...........................................62
152152+ 11.1. OAuth Access Token Types Registry ........................62
153153+ 11.1.1. Registration Template .............................62
154154+ 11.2. OAuth Parameters Registry ................................63
155155+ 11.2.1. Registration Template .............................63
156156+ 11.2.2. Initial Registry Contents .........................64
157157+ 11.3. OAuth Authorization Endpoint Response Types Registry .....66
158158+ 11.3.1. Registration Template .............................66
159159+ 11.3.2. Initial Registry Contents .........................67
160160+ 11.4. OAuth Extensions Error Registry ..........................67
161161+ 11.4.1. Registration Template .............................68
162162+ 12. References ....................................................68
163163+ 12.1. Normative References .....................................68
164164+ 12.2. Informative References ...................................70
165165+166166+167167+168168+169169+170170+Hardt Standards Track [Page 3]
171171+172172+RFC 6749 OAuth 2.0 October 2012
173173+174174+175175+ Appendix A. Augmented Backus-Naur Form (ABNF) Syntax ..............71
176176+ A.1. "client_id" Syntax ........................................71
177177+ A.2. "client_secret" Syntax ....................................71
178178+ A.3. "response_type" Syntax ....................................71
179179+ A.4. "scope" Syntax ............................................72
180180+ A.5. "state" Syntax ............................................72
181181+ A.6. "redirect_uri" Syntax .....................................72
182182+ A.7. "error" Syntax ............................................72
183183+ A.8. "error_description" Syntax ................................72
184184+ A.9. "error_uri" Syntax ........................................72
185185+ A.10. "grant_type" Syntax .......................................73
186186+ A.11. "code" Syntax .............................................73
187187+ A.12. "access_token" Syntax .....................................73
188188+ A.13. "token_type" Syntax .......................................73
189189+ A.14. "expires_in" Syntax .......................................73
190190+ A.15. "username" Syntax .........................................73
191191+ A.16. "password" Syntax .........................................73
192192+ A.17. "refresh_token" Syntax ....................................74
193193+ A.18. Endpoint Parameter Syntax .................................74
194194+ Appendix B. Use of application/x-www-form-urlencoded Media Type ...74
195195+ Appendix C. Acknowledgements ......................................75
196196+197197+1. Introduction
198198+199199+ In the traditional client-server authentication model, the client
200200+ requests an access-restricted resource (protected resource) on the
201201+ server by authenticating with the server using the resource owner's
202202+ credentials. In order to provide third-party applications access to
203203+ restricted resources, the resource owner shares its credentials with
204204+ the third party. This creates several problems and limitations:
205205+206206+ o Third-party applications are required to store the resource
207207+ owner's credentials for future use, typically a password in
208208+ clear-text.
209209+210210+ o Servers are required to support password authentication, despite
211211+ the security weaknesses inherent in passwords.
212212+213213+ o Third-party applications gain overly broad access to the resource
214214+ owner's protected resources, leaving resource owners without any
215215+ ability to restrict duration or access to a limited subset of
216216+ resources.
217217+218218+ o Resource owners cannot revoke access to an individual third party
219219+ without revoking access to all third parties, and must do so by
220220+ changing the third party's password.
221221+222222+223223+224224+225225+226226+Hardt Standards Track [Page 4]
227227+228228+RFC 6749 OAuth 2.0 October 2012
229229+230230+231231+ o Compromise of any third-party application results in compromise of
232232+ the end-user's password and all of the data protected by that
233233+ password.
234234+235235+ OAuth addresses these issues by introducing an authorization layer
236236+ and separating the role of the client from that of the resource
237237+ owner. In OAuth, the client requests access to resources controlled
238238+ by the resource owner and hosted by the resource server, and is
239239+ issued a different set of credentials than those of the resource
240240+ owner.
241241+242242+ Instead of using the resource owner's credentials to access protected
243243+ resources, the client obtains an access token -- a string denoting a
244244+ specific scope, lifetime, and other access attributes. Access tokens
245245+ are issued to third-party clients by an authorization server with the
246246+ approval of the resource owner. The client uses the access token to
247247+ access the protected resources hosted by the resource server.
248248+249249+ For example, an end-user (resource owner) can grant a printing
250250+ service (client) access to her protected photos stored at a photo-
251251+ sharing service (resource server), without sharing her username and
252252+ password with the printing service. Instead, she authenticates
253253+ directly with a server trusted by the photo-sharing service
254254+ (authorization server), which issues the printing service delegation-
255255+ specific credentials (access token).
256256+257257+ This specification is designed for use with HTTP ([RFC2616]). The
258258+ use of OAuth over any protocol other than HTTP is out of scope.
259259+260260+ The OAuth 1.0 protocol ([RFC5849]), published as an informational
261261+ document, was the result of a small ad hoc community effort. This
262262+ Standards Track specification builds on the OAuth 1.0 deployment
263263+ experience, as well as additional use cases and extensibility
264264+ requirements gathered from the wider IETF community. The OAuth 2.0
265265+ protocol is not backward compatible with OAuth 1.0. The two versions
266266+ may co-exist on the network, and implementations may choose to
267267+ support both. However, it is the intention of this specification
268268+ that new implementations support OAuth 2.0 as specified in this
269269+ document and that OAuth 1.0 is used only to support existing
270270+ deployments. The OAuth 2.0 protocol shares very few implementation
271271+ details with the OAuth 1.0 protocol. Implementers familiar with
272272+ OAuth 1.0 should approach this document without any assumptions as to
273273+ its structure and details.
274274+275275+276276+277277+278278+279279+280280+281281+282282+Hardt Standards Track [Page 5]
283283+284284+RFC 6749 OAuth 2.0 October 2012
285285+286286+287287+1.1. Roles
288288+289289+ OAuth defines four roles:
290290+291291+ resource owner
292292+ An entity capable of granting access to a protected resource.
293293+ When the resource owner is a person, it is referred to as an
294294+ end-user.
295295+296296+ resource server
297297+ The server hosting the protected resources, capable of accepting
298298+ and responding to protected resource requests using access tokens.
299299+300300+ client
301301+ An application making protected resource requests on behalf of the
302302+ resource owner and with its authorization. The term "client" does
303303+ not imply any particular implementation characteristics (e.g.,
304304+ whether the application executes on a server, a desktop, or other
305305+ devices).
306306+307307+ authorization server
308308+ The server issuing access tokens to the client after successfully
309309+ authenticating the resource owner and obtaining authorization.
310310+311311+ The interaction between the authorization server and resource server
312312+ is beyond the scope of this specification. The authorization server
313313+ may be the same server as the resource server or a separate entity.
314314+ A single authorization server may issue access tokens accepted by
315315+ multiple resource servers.
316316+317317+318318+319319+320320+321321+322322+323323+324324+325325+326326+327327+328328+329329+330330+331331+332332+333333+334334+335335+336336+337337+338338+Hardt Standards Track [Page 6]
339339+340340+RFC 6749 OAuth 2.0 October 2012
341341+342342+343343+1.2. Protocol Flow
344344+345345+ +--------+ +---------------+
346346+ | |--(A)- Authorization Request ->| Resource |
347347+ | | | Owner |
348348+ | |<-(B)-- Authorization Grant ---| |
349349+ | | +---------------+
350350+ | |
351351+ | | +---------------+
352352+ | |--(C)-- Authorization Grant -->| Authorization |
353353+ | Client | | Server |
354354+ | |<-(D)----- Access Token -------| |
355355+ | | +---------------+
356356+ | |
357357+ | | +---------------+
358358+ | |--(E)----- Access Token ------>| Resource |
359359+ | | | Server |
360360+ | |<-(F)--- Protected Resource ---| |
361361+ +--------+ +---------------+
362362+363363+ Figure 1: Abstract Protocol Flow
364364+365365+ The abstract OAuth 2.0 flow illustrated in Figure 1 describes the
366366+ interaction between the four roles and includes the following steps:
367367+368368+ (A) The client requests authorization from the resource owner. The
369369+ authorization request can be made directly to the resource owner
370370+ (as shown), or preferably indirectly via the authorization
371371+ server as an intermediary.
372372+373373+ (B) The client receives an authorization grant, which is a
374374+ credential representing the resource owner's authorization,
375375+ expressed using one of four grant types defined in this
376376+ specification or using an extension grant type. The
377377+ authorization grant type depends on the method used by the
378378+ client to request authorization and the types supported by the
379379+ authorization server.
380380+381381+ (C) The client requests an access token by authenticating with the
382382+ authorization server and presenting the authorization grant.
383383+384384+ (D) The authorization server authenticates the client and validates
385385+ the authorization grant, and if valid, issues an access token.
386386+387387+388388+389389+390390+391391+392392+393393+394394+Hardt Standards Track [Page 7]
395395+396396+RFC 6749 OAuth 2.0 October 2012
397397+398398+399399+ (E) The client requests the protected resource from the resource
400400+ server and authenticates by presenting the access token.
401401+402402+ (F) The resource server validates the access token, and if valid,
403403+ serves the request.
404404+405405+ The preferred method for the client to obtain an authorization grant
406406+ from the resource owner (depicted in steps (A) and (B)) is to use the
407407+ authorization server as an intermediary, which is illustrated in
408408+ Figure 3 in Section 4.1.
409409+410410+1.3. Authorization Grant
411411+412412+ An authorization grant is a credential representing the resource
413413+ owner's authorization (to access its protected resources) used by the
414414+ client to obtain an access token. This specification defines four
415415+ grant types -- authorization code, implicit, resource owner password
416416+ credentials, and client credentials -- as well as an extensibility
417417+ mechanism for defining additional types.
418418+419419+1.3.1. Authorization Code
420420+421421+ The authorization code is obtained by using an authorization server
422422+ as an intermediary between the client and resource owner. Instead of
423423+ requesting authorization directly from the resource owner, the client
424424+ directs the resource owner to an authorization server (via its
425425+ user-agent as defined in [RFC2616]), which in turn directs the
426426+ resource owner back to the client with the authorization code.
427427+428428+ Before directing the resource owner back to the client with the
429429+ authorization code, the authorization server authenticates the
430430+ resource owner and obtains authorization. Because the resource owner
431431+ only authenticates with the authorization server, the resource
432432+ owner's credentials are never shared with the client.
433433+434434+ The authorization code provides a few important security benefits,
435435+ such as the ability to authenticate the client, as well as the
436436+ transmission of the access token directly to the client without
437437+ passing it through the resource owner's user-agent and potentially
438438+ exposing it to others, including the resource owner.
439439+440440+1.3.2. Implicit
441441+442442+ The implicit grant is a simplified authorization code flow optimized
443443+ for clients implemented in a browser using a scripting language such
444444+ as JavaScript. In the implicit flow, instead of issuing the client
445445+ an authorization code, the client is issued an access token directly
446446+447447+448448+449449+450450+Hardt Standards Track [Page 8]
451451+452452+RFC 6749 OAuth 2.0 October 2012
453453+454454+455455+ (as the result of the resource owner authorization). The grant type
456456+ is implicit, as no intermediate credentials (such as an authorization
457457+ code) are issued (and later used to obtain an access token).
458458+459459+ When issuing an access token during the implicit grant flow, the
460460+ authorization server does not authenticate the client. In some
461461+ cases, the client identity can be verified via the redirection URI
462462+ used to deliver the access token to the client. The access token may
463463+ be exposed to the resource owner or other applications with access to
464464+ the resource owner's user-agent.
465465+466466+ Implicit grants improve the responsiveness and efficiency of some
467467+ clients (such as a client implemented as an in-browser application),
468468+ since it reduces the number of round trips required to obtain an
469469+ access token. However, this convenience should be weighed against
470470+ the security implications of using implicit grants, such as those
471471+ described in Sections 10.3 and 10.16, especially when the
472472+ authorization code grant type is available.
473473+474474+1.3.3. Resource Owner Password Credentials
475475+476476+ The resource owner password credentials (i.e., username and password)
477477+ can be used directly as an authorization grant to obtain an access
478478+ token. The credentials should only be used when there is a high
479479+ degree of trust between the resource owner and the client (e.g., the
480480+ client is part of the device operating system or a highly privileged
481481+ application), and when other authorization grant types are not
482482+ available (such as an authorization code).
483483+484484+ Even though this grant type requires direct client access to the
485485+ resource owner credentials, the resource owner credentials are used
486486+ for a single request and are exchanged for an access token. This
487487+ grant type can eliminate the need for the client to store the
488488+ resource owner credentials for future use, by exchanging the
489489+ credentials with a long-lived access token or refresh token.
490490+491491+1.3.4. Client Credentials
492492+493493+ The client credentials (or other forms of client authentication) can
494494+ be used as an authorization grant when the authorization scope is
495495+ limited to the protected resources under the control of the client,
496496+ or to protected resources previously arranged with the authorization
497497+ server. Client credentials are used as an authorization grant
498498+ typically when the client is acting on its own behalf (the client is
499499+ also the resource owner) or is requesting access to protected
500500+ resources based on an authorization previously arranged with the
501501+ authorization server.
502502+503503+504504+505505+506506+Hardt Standards Track [Page 9]
507507+508508+RFC 6749 OAuth 2.0 October 2012
509509+510510+511511+1.4. Access Token
512512+513513+ Access tokens are credentials used to access protected resources. An
514514+ access token is a string representing an authorization issued to the
515515+ client. The string is usually opaque to the client. Tokens
516516+ represent specific scopes and durations of access, granted by the
517517+ resource owner, and enforced by the resource server and authorization
518518+ server.
519519+520520+ The token may denote an identifier used to retrieve the authorization
521521+ information or may self-contain the authorization information in a
522522+ verifiable manner (i.e., a token string consisting of some data and a
523523+ signature). Additional authentication credentials, which are beyond
524524+ the scope of this specification, may be required in order for the
525525+ client to use a token.
526526+527527+ The access token provides an abstraction layer, replacing different
528528+ authorization constructs (e.g., username and password) with a single
529529+ token understood by the resource server. This abstraction enables
530530+ issuing access tokens more restrictive than the authorization grant
531531+ used to obtain them, as well as removing the resource server's need
532532+ to understand a wide range of authentication methods.
533533+534534+ Access tokens can have different formats, structures, and methods of
535535+ utilization (e.g., cryptographic properties) based on the resource
536536+ server security requirements. Access token attributes and the
537537+ methods used to access protected resources are beyond the scope of
538538+ this specification and are defined by companion specifications such
539539+ as [RFC6750].
540540+541541+1.5. Refresh Token
542542+543543+ Refresh tokens are credentials used to obtain access tokens. Refresh
544544+ tokens are issued to the client by the authorization server and are
545545+ used to obtain a new access token when the current access token
546546+ becomes invalid or expires, or to obtain additional access tokens
547547+ with identical or narrower scope (access tokens may have a shorter
548548+ lifetime and fewer permissions than authorized by the resource
549549+ owner). Issuing a refresh token is optional at the discretion of the
550550+ authorization server. If the authorization server issues a refresh
551551+ token, it is included when issuing an access token (i.e., step (D) in
552552+ Figure 1).
553553+554554+ A refresh token is a string representing the authorization granted to
555555+ the client by the resource owner. The string is usually opaque to
556556+ the client. The token denotes an identifier used to retrieve the
557557+558558+559559+560560+561561+562562+Hardt Standards Track [Page 10]
563563+564564+RFC 6749 OAuth 2.0 October 2012
565565+566566+567567+ authorization information. Unlike access tokens, refresh tokens are
568568+ intended for use only with authorization servers and are never sent
569569+ to resource servers.
570570+571571+ +--------+ +---------------+
572572+ | |--(A)------- Authorization Grant --------->| |
573573+ | | | |
574574+ | |<-(B)----------- Access Token -------------| |
575575+ | | & Refresh Token | |
576576+ | | | |
577577+ | | +----------+ | |
578578+ | |--(C)---- Access Token ---->| | | |
579579+ | | | | | |
580580+ | |<-(D)- Protected Resource --| Resource | | Authorization |
581581+ | Client | | Server | | Server |
582582+ | |--(E)---- Access Token ---->| | | |
583583+ | | | | | |
584584+ | |<-(F)- Invalid Token Error -| | | |
585585+ | | +----------+ | |
586586+ | | | |
587587+ | |--(G)----------- Refresh Token ----------->| |
588588+ | | | |
589589+ | |<-(H)----------- Access Token -------------| |
590590+ +--------+ & Optional Refresh Token +---------------+
591591+592592+ Figure 2: Refreshing an Expired Access Token
593593+594594+ The flow illustrated in Figure 2 includes the following steps:
595595+596596+ (A) The client requests an access token by authenticating with the
597597+ authorization server and presenting an authorization grant.
598598+599599+ (B) The authorization server authenticates the client and validates
600600+ the authorization grant, and if valid, issues an access token
601601+ and a refresh token.
602602+603603+ (C) The client makes a protected resource request to the resource
604604+ server by presenting the access token.
605605+606606+ (D) The resource server validates the access token, and if valid,
607607+ serves the request.
608608+609609+ (E) Steps (C) and (D) repeat until the access token expires. If the
610610+ client knows the access token expired, it skips to step (G);
611611+ otherwise, it makes another protected resource request.
612612+613613+ (F) Since the access token is invalid, the resource server returns
614614+ an invalid token error.
615615+616616+617617+618618+Hardt Standards Track [Page 11]
619619+620620+RFC 6749 OAuth 2.0 October 2012
621621+622622+623623+ (G) The client requests a new access token by authenticating with
624624+ the authorization server and presenting the refresh token. The
625625+ client authentication requirements are based on the client type
626626+ and on the authorization server policies.
627627+628628+ (H) The authorization server authenticates the client and validates
629629+ the refresh token, and if valid, issues a new access token (and,
630630+ optionally, a new refresh token).
631631+632632+ Steps (C), (D), (E), and (F) are outside the scope of this
633633+ specification, as described in Section 7.
634634+635635+1.6. TLS Version
636636+637637+ Whenever Transport Layer Security (TLS) is used by this
638638+ specification, the appropriate version (or versions) of TLS will vary
639639+ over time, based on the widespread deployment and known security
640640+ vulnerabilities. At the time of this writing, TLS version 1.2
641641+ [RFC5246] is the most recent version, but has a very limited
642642+ deployment base and might not be readily available for
643643+ implementation. TLS version 1.0 [RFC2246] is the most widely
644644+ deployed version and will provide the broadest interoperability.
645645+646646+ Implementations MAY also support additional transport-layer security
647647+ mechanisms that meet their security requirements.
648648+649649+1.7. HTTP Redirections
650650+651651+ This specification makes extensive use of HTTP redirections, in which
652652+ the client or the authorization server directs the resource owner's
653653+ user-agent to another destination. While the examples in this
654654+ specification show the use of the HTTP 302 status code, any other
655655+ method available via the user-agent to accomplish this redirection is
656656+ allowed and is considered to be an implementation detail.
657657+658658+1.8. Interoperability
659659+660660+ OAuth 2.0 provides a rich authorization framework with well-defined
661661+ security properties. However, as a rich and highly extensible
662662+ framework with many optional components, on its own, this
663663+ specification is likely to produce a wide range of non-interoperable
664664+ implementations.
665665+666666+ In addition, this specification leaves a few required components
667667+ partially or fully undefined (e.g., client registration,
668668+ authorization server capabilities, endpoint discovery). Without
669669+670670+671671+672672+673673+674674+Hardt Standards Track [Page 12]
675675+676676+RFC 6749 OAuth 2.0 October 2012
677677+678678+679679+ these components, clients must be manually and specifically
680680+ configured against a specific authorization server and resource
681681+ server in order to interoperate.
682682+683683+ This framework was designed with the clear expectation that future
684684+ work will define prescriptive profiles and extensions necessary to
685685+ achieve full web-scale interoperability.
686686+687687+1.9. Notational Conventions
688688+689689+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
690690+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this
691691+ specification are to be interpreted as described in [RFC2119].
692692+693693+ This specification uses the Augmented Backus-Naur Form (ABNF)
694694+ notation of [RFC5234]. Additionally, the rule URI-reference is
695695+ included from "Uniform Resource Identifier (URI): Generic Syntax"
696696+ [RFC3986].
697697+698698+ Certain security-related terms are to be understood in the sense
699699+ defined in [RFC4949]. These terms include, but are not limited to,
700700+ "attack", "authentication", "authorization", "certificate",
701701+ "confidentiality", "credential", "encryption", "identity", "sign",
702702+ "signature", "trust", "validate", and "verify".
703703+704704+ Unless otherwise noted, all the protocol parameter names and values
705705+ are case sensitive.
706706+707707+2. Client Registration
708708+709709+ Before initiating the protocol, the client registers with the
710710+ authorization server. The means through which the client registers
711711+ with the authorization server are beyond the scope of this
712712+ specification but typically involve end-user interaction with an HTML
713713+ registration form.
714714+715715+ Client registration does not require a direct interaction between the
716716+ client and the authorization server. When supported by the
717717+ authorization server, registration can rely on other means for
718718+ establishing trust and obtaining the required client properties
719719+ (e.g., redirection URI, client type). For example, registration can
720720+ be accomplished using a self-issued or third-party-issued assertion,
721721+ or by the authorization server performing client discovery using a
722722+ trusted channel.
723723+724724+725725+726726+727727+728728+729729+730730+Hardt Standards Track [Page 13]
731731+732732+RFC 6749 OAuth 2.0 October 2012
733733+734734+735735+ When registering a client, the client developer SHALL:
736736+737737+ o specify the client type as described in Section 2.1,
738738+739739+ o provide its client redirection URIs as described in Section 3.1.2,
740740+ and
741741+742742+ o include any other information required by the authorization server
743743+ (e.g., application name, website, description, logo image, the
744744+ acceptance of legal terms).
745745+746746+2.1. Client Types
747747+748748+ OAuth defines two client types, based on their ability to
749749+ authenticate securely with the authorization server (i.e., ability to
750750+ maintain the confidentiality of their client credentials):
751751+752752+ confidential
753753+ Clients capable of maintaining the confidentiality of their
754754+ credentials (e.g., client implemented on a secure server with
755755+ restricted access to the client credentials), or capable of secure
756756+ client authentication using other means.
757757+758758+ public
759759+ Clients incapable of maintaining the confidentiality of their
760760+ credentials (e.g., clients executing on the device used by the
761761+ resource owner, such as an installed native application or a web
762762+ browser-based application), and incapable of secure client
763763+ authentication via any other means.
764764+765765+ The client type designation is based on the authorization server's
766766+ definition of secure authentication and its acceptable exposure
767767+ levels of client credentials. The authorization server SHOULD NOT
768768+ make assumptions about the client type.
769769+770770+ A client may be implemented as a distributed set of components, each
771771+ with a different client type and security context (e.g., a
772772+ distributed client with both a confidential server-based component
773773+ and a public browser-based component). If the authorization server
774774+ does not provide support for such clients or does not provide
775775+ guidance with regard to their registration, the client SHOULD
776776+ register each component as a separate client.
777777+778778+779779+780780+781781+782782+783783+784784+785785+786786+Hardt Standards Track [Page 14]
787787+788788+RFC 6749 OAuth 2.0 October 2012
789789+790790+791791+ This specification has been designed around the following client
792792+ profiles:
793793+794794+ web application
795795+ A web application is a confidential client running on a web
796796+ server. Resource owners access the client via an HTML user
797797+ interface rendered in a user-agent on the device used by the
798798+ resource owner. The client credentials as well as any access
799799+ token issued to the client are stored on the web server and are
800800+ not exposed to or accessible by the resource owner.
801801+802802+ user-agent-based application
803803+ A user-agent-based application is a public client in which the
804804+ client code is downloaded from a web server and executes within a
805805+ user-agent (e.g., web browser) on the device used by the resource
806806+ owner. Protocol data and credentials are easily accessible (and
807807+ often visible) to the resource owner. Since such applications
808808+ reside within the user-agent, they can make seamless use of the
809809+ user-agent capabilities when requesting authorization.
810810+811811+ native application
812812+ A native application is a public client installed and executed on
813813+ the device used by the resource owner. Protocol data and
814814+ credentials are accessible to the resource owner. It is assumed
815815+ that any client authentication credentials included in the
816816+ application can be extracted. On the other hand, dynamically
817817+ issued credentials such as access tokens or refresh tokens can
818818+ receive an acceptable level of protection. At a minimum, these
819819+ credentials are protected from hostile servers with which the
820820+ application may interact. On some platforms, these credentials
821821+ might be protected from other applications residing on the same
822822+ device.
823823+824824+2.2. Client Identifier
825825+826826+ The authorization server issues the registered client a client
827827+ identifier -- a unique string representing the registration
828828+ information provided by the client. The client identifier is not a
829829+ secret; it is exposed to the resource owner and MUST NOT be used
830830+ alone for client authentication. The client identifier is unique to
831831+ the authorization server.
832832+833833+ The client identifier string size is left undefined by this
834834+ specification. The client should avoid making assumptions about the
835835+ identifier size. The authorization server SHOULD document the size
836836+ of any identifier it issues.
837837+838838+839839+840840+841841+842842+Hardt Standards Track [Page 15]
843843+844844+RFC 6749 OAuth 2.0 October 2012
845845+846846+847847+2.3. Client Authentication
848848+849849+ If the client type is confidential, the client and authorization
850850+ server establish a client authentication method suitable for the
851851+ security requirements of the authorization server. The authorization
852852+ server MAY accept any form of client authentication meeting its
853853+ security requirements.
854854+855855+ Confidential clients are typically issued (or establish) a set of
856856+ client credentials used for authenticating with the authorization
857857+ server (e.g., password, public/private key pair).
858858+859859+ The authorization server MAY establish a client authentication method
860860+ with public clients. However, the authorization server MUST NOT rely
861861+ on public client authentication for the purpose of identifying the
862862+ client.
863863+864864+ The client MUST NOT use more than one authentication method in each
865865+ request.
866866+867867+2.3.1. Client Password
868868+869869+ Clients in possession of a client password MAY use the HTTP Basic
870870+ authentication scheme as defined in [RFC2617] to authenticate with
871871+ the authorization server. The client identifier is encoded using the
872872+ "application/x-www-form-urlencoded" encoding algorithm per
873873+ Appendix B, and the encoded value is used as the username; the client
874874+ password is encoded using the same algorithm and used as the
875875+ password. The authorization server MUST support the HTTP Basic
876876+ authentication scheme for authenticating clients that were issued a
877877+ client password.
878878+879879+ For example (with extra line breaks for display purposes only):
880880+881881+ Authorization: Basic czZCaGRSa3F0Mzo3RmpmcDBaQnIxS3REUmJuZlZkbUl3
882882+883883+ Alternatively, the authorization server MAY support including the
884884+ client credentials in the request-body using the following
885885+ parameters:
886886+887887+ client_id
888888+ REQUIRED. The client identifier issued to the client during
889889+ the registration process described by Section 2.2.
890890+891891+ client_secret
892892+ REQUIRED. The client secret. The client MAY omit the
893893+ parameter if the client secret is an empty string.
894894+895895+896896+897897+898898+Hardt Standards Track [Page 16]
899899+900900+RFC 6749 OAuth 2.0 October 2012
901901+902902+903903+ Including the client credentials in the request-body using the two
904904+ parameters is NOT RECOMMENDED and SHOULD be limited to clients unable
905905+ to directly utilize the HTTP Basic authentication scheme (or other
906906+ password-based HTTP authentication schemes). The parameters can only
907907+ be transmitted in the request-body and MUST NOT be included in the
908908+ request URI.
909909+910910+ For example, a request to refresh an access token (Section 6) using
911911+ the body parameters (with extra line breaks for display purposes
912912+ only):
913913+914914+ POST /token HTTP/1.1
915915+ Host: server.example.com
916916+ Content-Type: application/x-www-form-urlencoded
917917+918918+ grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
919919+ &client_id=s6BhdRkqt3&client_secret=7Fjfp0ZBr1KtDRbnfVdmIw
920920+921921+ The authorization server MUST require the use of TLS as described in
922922+ Section 1.6 when sending requests using password authentication.
923923+924924+ Since this client authentication method involves a password, the
925925+ authorization server MUST protect any endpoint utilizing it against
926926+ brute force attacks.
927927+928928+2.3.2. Other Authentication Methods
929929+930930+ The authorization server MAY support any suitable HTTP authentication
931931+ scheme matching its security requirements. When using other
932932+ authentication methods, the authorization server MUST define a
933933+ mapping between the client identifier (registration record) and
934934+ authentication scheme.
935935+936936+2.4. Unregistered Clients
937937+938938+ This specification does not exclude the use of unregistered clients.
939939+ However, the use of such clients is beyond the scope of this
940940+ specification and requires additional security analysis and review of
941941+ its interoperability impact.
942942+943943+944944+945945+946946+947947+948948+949949+950950+951951+952952+953953+954954+Hardt Standards Track [Page 17]
955955+956956+RFC 6749 OAuth 2.0 October 2012
957957+958958+959959+3. Protocol Endpoints
960960+961961+ The authorization process utilizes two authorization server endpoints
962962+ (HTTP resources):
963963+964964+ o Authorization endpoint - used by the client to obtain
965965+ authorization from the resource owner via user-agent redirection.
966966+967967+ o Token endpoint - used by the client to exchange an authorization
968968+ grant for an access token, typically with client authentication.
969969+970970+ As well as one client endpoint:
971971+972972+ o Redirection endpoint - used by the authorization server to return
973973+ responses containing authorization credentials to the client via
974974+ the resource owner user-agent.
975975+976976+ Not every authorization grant type utilizes both endpoints.
977977+ Extension grant types MAY define additional endpoints as needed.
978978+979979+3.1. Authorization Endpoint
980980+981981+ The authorization endpoint is used to interact with the resource
982982+ owner and obtain an authorization grant. The authorization server
983983+ MUST first verify the identity of the resource owner. The way in
984984+ which the authorization server authenticates the resource owner
985985+ (e.g., username and password login, session cookies) is beyond the
986986+ scope of this specification.
987987+988988+ The means through which the client obtains the location of the
989989+ authorization endpoint are beyond the scope of this specification,
990990+ but the location is typically provided in the service documentation.
991991+992992+ The endpoint URI MAY include an "application/x-www-form-urlencoded"
993993+ formatted (per Appendix B) query component ([RFC3986] Section 3.4),
994994+ which MUST be retained when adding additional query parameters. The
995995+ endpoint URI MUST NOT include a fragment component.
996996+997997+ Since requests to the authorization endpoint result in user
998998+ authentication and the transmission of clear-text credentials (in the
999999+ HTTP response), the authorization server MUST require the use of TLS
10001000+ as described in Section 1.6 when sending requests to the
10011001+ authorization endpoint.
10021002+10031003+ The authorization server MUST support the use of the HTTP "GET"
10041004+ method [RFC2616] for the authorization endpoint and MAY support the
10051005+ use of the "POST" method as well.
10061006+10071007+10081008+10091009+10101010+Hardt Standards Track [Page 18]
10111011+10121012+RFC 6749 OAuth 2.0 October 2012
10131013+10141014+10151015+ Parameters sent without a value MUST be treated as if they were
10161016+ omitted from the request. The authorization server MUST ignore
10171017+ unrecognized request parameters. Request and response parameters
10181018+ MUST NOT be included more than once.
10191019+10201020+3.1.1. Response Type
10211021+10221022+ The authorization endpoint is used by the authorization code grant
10231023+ type and implicit grant type flows. The client informs the
10241024+ authorization server of the desired grant type using the following
10251025+ parameter:
10261026+10271027+ response_type
10281028+ REQUIRED. The value MUST be one of "code" for requesting an
10291029+ authorization code as described by Section 4.1.1, "token" for
10301030+ requesting an access token (implicit grant) as described by
10311031+ Section 4.2.1, or a registered extension value as described by
10321032+ Section 8.4.
10331033+10341034+ Extension response types MAY contain a space-delimited (%x20) list of
10351035+ values, where the order of values does not matter (e.g., response
10361036+ type "a b" is the same as "b a"). The meaning of such composite
10371037+ response types is defined by their respective specifications.
10381038+10391039+ If an authorization request is missing the "response_type" parameter,
10401040+ or if the response type is not understood, the authorization server
10411041+ MUST return an error response as described in Section 4.1.2.1.
10421042+10431043+3.1.2. Redirection Endpoint
10441044+10451045+ After completing its interaction with the resource owner, the
10461046+ authorization server directs the resource owner's user-agent back to
10471047+ the client. The authorization server redirects the user-agent to the
10481048+ client's redirection endpoint previously established with the
10491049+ authorization server during the client registration process or when
10501050+ making the authorization request.
10511051+10521052+ The redirection endpoint URI MUST be an absolute URI as defined by
10531053+ [RFC3986] Section 4.3. The endpoint URI MAY include an
10541054+ "application/x-www-form-urlencoded" formatted (per Appendix B) query
10551055+ component ([RFC3986] Section 3.4), which MUST be retained when adding
10561056+ additional query parameters. The endpoint URI MUST NOT include a
10571057+ fragment component.
10581058+10591059+10601060+10611061+10621062+10631063+10641064+10651065+10661066+Hardt Standards Track [Page 19]
10671067+10681068+RFC 6749 OAuth 2.0 October 2012
10691069+10701070+10711071+3.1.2.1. Endpoint Request Confidentiality
10721072+10731073+ The redirection endpoint SHOULD require the use of TLS as described
10741074+ in Section 1.6 when the requested response type is "code" or "token",
10751075+ or when the redirection request will result in the transmission of
10761076+ sensitive credentials over an open network. This specification does
10771077+ not mandate the use of TLS because at the time of this writing,
10781078+ requiring clients to deploy TLS is a significant hurdle for many
10791079+ client developers. If TLS is not available, the authorization server
10801080+ SHOULD warn the resource owner about the insecure endpoint prior to
10811081+ redirection (e.g., display a message during the authorization
10821082+ request).
10831083+10841084+ Lack of transport-layer security can have a severe impact on the
10851085+ security of the client and the protected resources it is authorized
10861086+ to access. The use of transport-layer security is particularly
10871087+ critical when the authorization process is used as a form of
10881088+ delegated end-user authentication by the client (e.g., third-party
10891089+ sign-in service).
10901090+10911091+3.1.2.2. Registration Requirements
10921092+10931093+ The authorization server MUST require the following clients to
10941094+ register their redirection endpoint:
10951095+10961096+ o Public clients.
10971097+10981098+ o Confidential clients utilizing the implicit grant type.
10991099+11001100+ The authorization server SHOULD require all clients to register their
11011101+ redirection endpoint prior to utilizing the authorization endpoint.
11021102+11031103+ The authorization server SHOULD require the client to provide the
11041104+ complete redirection URI (the client MAY use the "state" request
11051105+ parameter to achieve per-request customization). If requiring the
11061106+ registration of the complete redirection URI is not possible, the
11071107+ authorization server SHOULD require the registration of the URI
11081108+ scheme, authority, and path (allowing the client to dynamically vary
11091109+ only the query component of the redirection URI when requesting
11101110+ authorization).
11111111+11121112+ The authorization server MAY allow the client to register multiple
11131113+ redirection endpoints.
11141114+11151115+ Lack of a redirection URI registration requirement can enable an
11161116+ attacker to use the authorization endpoint as an open redirector as
11171117+ described in Section 10.15.
11181118+11191119+11201120+11211121+11221122+Hardt Standards Track [Page 20]
11231123+11241124+RFC 6749 OAuth 2.0 October 2012
11251125+11261126+11271127+3.1.2.3. Dynamic Configuration
11281128+11291129+ If multiple redirection URIs have been registered, if only part of
11301130+ the redirection URI has been registered, or if no redirection URI has
11311131+ been registered, the client MUST include a redirection URI with the
11321132+ authorization request using the "redirect_uri" request parameter.
11331133+11341134+ When a redirection URI is included in an authorization request, the
11351135+ authorization server MUST compare and match the value received
11361136+ against at least one of the registered redirection URIs (or URI
11371137+ components) as defined in [RFC3986] Section 6, if any redirection
11381138+ URIs were registered. If the client registration included the full
11391139+ redirection URI, the authorization server MUST compare the two URIs
11401140+ using simple string comparison as defined in [RFC3986] Section 6.2.1.
11411141+11421142+3.1.2.4. Invalid Endpoint
11431143+11441144+ If an authorization request fails validation due to a missing,
11451145+ invalid, or mismatching redirection URI, the authorization server
11461146+ SHOULD inform the resource owner of the error and MUST NOT
11471147+ automatically redirect the user-agent to the invalid redirection URI.
11481148+11491149+3.1.2.5. Endpoint Content
11501150+11511151+ The redirection request to the client's endpoint typically results in
11521152+ an HTML document response, processed by the user-agent. If the HTML
11531153+ response is served directly as the result of the redirection request,
11541154+ any script included in the HTML document will execute with full
11551155+ access to the redirection URI and the credentials it contains.
11561156+11571157+ The client SHOULD NOT include any third-party scripts (e.g., third-
11581158+ party analytics, social plug-ins, ad networks) in the redirection
11591159+ endpoint response. Instead, it SHOULD extract the credentials from
11601160+ the URI and redirect the user-agent again to another endpoint without
11611161+ exposing the credentials (in the URI or elsewhere). If third-party
11621162+ scripts are included, the client MUST ensure that its own scripts
11631163+ (used to extract and remove the credentials from the URI) will
11641164+ execute first.
11651165+11661166+3.2. Token Endpoint
11671167+11681168+ The token endpoint is used by the client to obtain an access token by
11691169+ presenting its authorization grant or refresh token. The token
11701170+ endpoint is used with every authorization grant except for the
11711171+ implicit grant type (since an access token is issued directly).
11721172+11731173+11741174+11751175+11761176+11771177+11781178+Hardt Standards Track [Page 21]
11791179+11801180+RFC 6749 OAuth 2.0 October 2012
11811181+11821182+11831183+ The means through which the client obtains the location of the token
11841184+ endpoint are beyond the scope of this specification, but the location
11851185+ is typically provided in the service documentation.
11861186+11871187+ The endpoint URI MAY include an "application/x-www-form-urlencoded"
11881188+ formatted (per Appendix B) query component ([RFC3986] Section 3.4),
11891189+ which MUST be retained when adding additional query parameters. The
11901190+ endpoint URI MUST NOT include a fragment component.
11911191+11921192+ Since requests to the token endpoint result in the transmission of
11931193+ clear-text credentials (in the HTTP request and response), the
11941194+ authorization server MUST require the use of TLS as described in
11951195+ Section 1.6 when sending requests to the token endpoint.
11961196+11971197+ The client MUST use the HTTP "POST" method when making access token
11981198+ requests.
11991199+12001200+ Parameters sent without a value MUST be treated as if they were
12011201+ omitted from the request. The authorization server MUST ignore
12021202+ unrecognized request parameters. Request and response parameters
12031203+ MUST NOT be included more than once.
12041204+12051205+3.2.1. Client Authentication
12061206+12071207+ Confidential clients or other clients issued client credentials MUST
12081208+ authenticate with the authorization server as described in
12091209+ Section 2.3 when making requests to the token endpoint. Client
12101210+ authentication is used for:
12111211+12121212+ o Enforcing the binding of refresh tokens and authorization codes to
12131213+ the client they were issued to. Client authentication is critical
12141214+ when an authorization code is transmitted to the redirection
12151215+ endpoint over an insecure channel or when the redirection URI has
12161216+ not been registered in full.
12171217+12181218+ o Recovering from a compromised client by disabling the client or
12191219+ changing its credentials, thus preventing an attacker from abusing
12201220+ stolen refresh tokens. Changing a single set of client
12211221+ credentials is significantly faster than revoking an entire set of
12221222+ refresh tokens.
12231223+12241224+ o Implementing authentication management best practices, which
12251225+ require periodic credential rotation. Rotation of an entire set
12261226+ of refresh tokens can be challenging, while rotation of a single
12271227+ set of client credentials is significantly easier.
12281228+12291229+12301230+12311231+12321232+12331233+12341234+Hardt Standards Track [Page 22]
12351235+12361236+RFC 6749 OAuth 2.0 October 2012
12371237+12381238+12391239+ A client MAY use the "client_id" request parameter to identify itself
12401240+ when sending requests to the token endpoint. In the
12411241+ "authorization_code" "grant_type" request to the token endpoint, an
12421242+ unauthenticated client MUST send its "client_id" to prevent itself
12431243+ from inadvertently accepting a code intended for a client with a
12441244+ different "client_id". This protects the client from substitution of
12451245+ the authentication code. (It provides no additional security for the
12461246+ protected resource.)
12471247+12481248+3.3. Access Token Scope
12491249+12501250+ The authorization and token endpoints allow the client to specify the
12511251+ scope of the access request using the "scope" request parameter. In
12521252+ turn, the authorization server uses the "scope" response parameter to
12531253+ inform the client of the scope of the access token issued.
12541254+12551255+ The value of the scope parameter is expressed as a list of space-
12561256+ delimited, case-sensitive strings. The strings are defined by the
12571257+ authorization server. If the value contains multiple space-delimited
12581258+ strings, their order does not matter, and each string adds an
12591259+ additional access range to the requested scope.
12601260+12611261+ scope = scope-token *( SP scope-token )
12621262+ scope-token = 1*( %x21 / %x23-5B / %x5D-7E )
12631263+12641264+ The authorization server MAY fully or partially ignore the scope
12651265+ requested by the client, based on the authorization server policy or
12661266+ the resource owner's instructions. If the issued access token scope
12671267+ is different from the one requested by the client, the authorization
12681268+ server MUST include the "scope" response parameter to inform the
12691269+ client of the actual scope granted.
12701270+12711271+ If the client omits the scope parameter when requesting
12721272+ authorization, the authorization server MUST either process the
12731273+ request using a pre-defined default value or fail the request
12741274+ indicating an invalid scope. The authorization server SHOULD
12751275+ document its scope requirements and default value (if defined).
12761276+12771277+4. Obtaining Authorization
12781278+12791279+ To request an access token, the client obtains authorization from the
12801280+ resource owner. The authorization is expressed in the form of an
12811281+ authorization grant, which the client uses to request the access
12821282+ token. OAuth defines four grant types: authorization code, implicit,
12831283+ resource owner password credentials, and client credentials. It also
12841284+ provides an extension mechanism for defining additional grant types.
12851285+12861286+12871287+12881288+12891289+12901290+Hardt Standards Track [Page 23]
12911291+12921292+RFC 6749 OAuth 2.0 October 2012
12931293+12941294+12951295+4.1. Authorization Code Grant
12961296+12971297+ The authorization code grant type is used to obtain both access
12981298+ tokens and refresh tokens and is optimized for confidential clients.
12991299+ Since this is a redirection-based flow, the client must be capable of
13001300+ interacting with the resource owner's user-agent (typically a web
13011301+ browser) and capable of receiving incoming requests (via redirection)
13021302+ from the authorization server.
13031303+13041304+ +----------+
13051305+ | Resource |
13061306+ | Owner |
13071307+ | |
13081308+ +----------+
13091309+ ^
13101310+ |
13111311+ (B)
13121312+ +----|-----+ Client Identifier +---------------+
13131313+ | -+----(A)-- & Redirection URI ---->| |
13141314+ | User- | | Authorization |
13151315+ | Agent -+----(B)-- User authenticates --->| Server |
13161316+ | | | |
13171317+ | -+----(C)-- Authorization Code ---<| |
13181318+ +-|----|---+ +---------------+
13191319+ | | ^ v
13201320+ (A) (C) | |
13211321+ | | | |
13221322+ ^ v | |
13231323+ +---------+ | |
13241324+ | |>---(D)-- Authorization Code ---------' |
13251325+ | Client | & Redirection URI |
13261326+ | | |
13271327+ | |<---(E)----- Access Token -------------------'
13281328+ +---------+ (w/ Optional Refresh Token)
13291329+13301330+ Note: The lines illustrating steps (A), (B), and (C) are broken into
13311331+ two parts as they pass through the user-agent.
13321332+13331333+ Figure 3: Authorization Code Flow
13341334+13351335+13361336+13371337+13381338+13391339+13401340+13411341+13421342+13431343+13441344+13451345+13461346+Hardt Standards Track [Page 24]
13471347+13481348+RFC 6749 OAuth 2.0 October 2012
13491349+13501350+13511351+ The flow illustrated in Figure 3 includes the following steps:
13521352+13531353+ (A) The client initiates the flow by directing the resource owner's
13541354+ user-agent to the authorization endpoint. The client includes
13551355+ its client identifier, requested scope, local state, and a
13561356+ redirection URI to which the authorization server will send the
13571357+ user-agent back once access is granted (or denied).
13581358+13591359+ (B) The authorization server authenticates the resource owner (via
13601360+ the user-agent) and establishes whether the resource owner
13611361+ grants or denies the client's access request.
13621362+13631363+ (C) Assuming the resource owner grants access, the authorization
13641364+ server redirects the user-agent back to the client using the
13651365+ redirection URI provided earlier (in the request or during
13661366+ client registration). The redirection URI includes an
13671367+ authorization code and any local state provided by the client
13681368+ earlier.
13691369+13701370+ (D) The client requests an access token from the authorization
13711371+ server's token endpoint by including the authorization code
13721372+ received in the previous step. When making the request, the
13731373+ client authenticates with the authorization server. The client
13741374+ includes the redirection URI used to obtain the authorization
13751375+ code for verification.
13761376+13771377+ (E) The authorization server authenticates the client, validates the
13781378+ authorization code, and ensures that the redirection URI
13791379+ received matches the URI used to redirect the client in
13801380+ step (C). If valid, the authorization server responds back with
13811381+ an access token and, optionally, a refresh token.
13821382+13831383+4.1.1. Authorization Request
13841384+13851385+ The client constructs the request URI by adding the following
13861386+ parameters to the query component of the authorization endpoint URI
13871387+ using the "application/x-www-form-urlencoded" format, per Appendix B:
13881388+13891389+ response_type
13901390+ REQUIRED. Value MUST be set to "code".
13911391+13921392+ client_id
13931393+ REQUIRED. The client identifier as described in Section 2.2.
13941394+13951395+ redirect_uri
13961396+ OPTIONAL. As described in Section 3.1.2.
13971397+13981398+13991399+14001400+14011401+14021402+Hardt Standards Track [Page 25]
14031403+14041404+RFC 6749 OAuth 2.0 October 2012
14051405+14061406+14071407+ scope
14081408+ OPTIONAL. The scope of the access request as described by
14091409+ Section 3.3.
14101410+14111411+ state
14121412+ RECOMMENDED. An opaque value used by the client to maintain
14131413+ state between the request and callback. The authorization
14141414+ server includes this value when redirecting the user-agent back
14151415+ to the client. The parameter SHOULD be used for preventing
14161416+ cross-site request forgery as described in Section 10.12.
14171417+14181418+ The client directs the resource owner to the constructed URI using an
14191419+ HTTP redirection response, or by other means available to it via the
14201420+ user-agent.
14211421+14221422+ For example, the client directs the user-agent to make the following
14231423+ HTTP request using TLS (with extra line breaks for display purposes
14241424+ only):
14251425+14261426+ GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz
14271427+ &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
14281428+ Host: server.example.com
14291429+14301430+ The authorization server validates the request to ensure that all
14311431+ required parameters are present and valid. If the request is valid,
14321432+ the authorization server authenticates the resource owner and obtains
14331433+ an authorization decision (by asking the resource owner or by
14341434+ establishing approval via other means).
14351435+14361436+ When a decision is established, the authorization server directs the
14371437+ user-agent to the provided client redirection URI using an HTTP
14381438+ redirection response, or by other means available to it via the
14391439+ user-agent.
14401440+14411441+4.1.2. Authorization Response
14421442+14431443+ If the resource owner grants the access request, the authorization
14441444+ server issues an authorization code and delivers it to the client by
14451445+ adding the following parameters to the query component of the
14461446+ redirection URI using the "application/x-www-form-urlencoded" format,
14471447+ per Appendix B:
14481448+14491449+ code
14501450+ REQUIRED. The authorization code generated by the
14511451+ authorization server. The authorization code MUST expire
14521452+ shortly after it is issued to mitigate the risk of leaks. A
14531453+ maximum authorization code lifetime of 10 minutes is
14541454+ RECOMMENDED. The client MUST NOT use the authorization code
14551455+14561456+14571457+14581458+Hardt Standards Track [Page 26]
14591459+14601460+RFC 6749 OAuth 2.0 October 2012
14611461+14621462+14631463+ more than once. If an authorization code is used more than
14641464+ once, the authorization server MUST deny the request and SHOULD
14651465+ revoke (when possible) all tokens previously issued based on
14661466+ that authorization code. The authorization code is bound to
14671467+ the client identifier and redirection URI.
14681468+14691469+ state
14701470+ REQUIRED if the "state" parameter was present in the client
14711471+ authorization request. The exact value received from the
14721472+ client.
14731473+14741474+ For example, the authorization server redirects the user-agent by
14751475+ sending the following HTTP response:
14761476+14771477+ HTTP/1.1 302 Found
14781478+ Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA
14791479+ &state=xyz
14801480+14811481+ The client MUST ignore unrecognized response parameters. The
14821482+ authorization code string size is left undefined by this
14831483+ specification. The client should avoid making assumptions about code
14841484+ value sizes. The authorization server SHOULD document the size of
14851485+ any value it issues.
14861486+14871487+4.1.2.1. Error Response
14881488+14891489+ If the request fails due to a missing, invalid, or mismatching
14901490+ redirection URI, or if the client identifier is missing or invalid,
14911491+ the authorization server SHOULD inform the resource owner of the
14921492+ error and MUST NOT automatically redirect the user-agent to the
14931493+ invalid redirection URI.
14941494+14951495+ If the resource owner denies the access request or if the request
14961496+ fails for reasons other than a missing or invalid redirection URI,
14971497+ the authorization server informs the client by adding the following
14981498+ parameters to the query component of the redirection URI using the
14991499+ "application/x-www-form-urlencoded" format, per Appendix B:
15001500+15011501+ error
15021502+ REQUIRED. A single ASCII [USASCII] error code from the
15031503+ following:
15041504+15051505+ invalid_request
15061506+ The request is missing a required parameter, includes an
15071507+ invalid parameter value, includes a parameter more than
15081508+ once, or is otherwise malformed.
15091509+15101510+15111511+15121512+15131513+15141514+Hardt Standards Track [Page 27]
15151515+15161516+RFC 6749 OAuth 2.0 October 2012
15171517+15181518+15191519+ unauthorized_client
15201520+ The client is not authorized to request an authorization
15211521+ code using this method.
15221522+15231523+ access_denied
15241524+ The resource owner or authorization server denied the
15251525+ request.
15261526+15271527+ unsupported_response_type
15281528+ The authorization server does not support obtaining an
15291529+ authorization code using this method.
15301530+15311531+ invalid_scope
15321532+ The requested scope is invalid, unknown, or malformed.
15331533+15341534+ server_error
15351535+ The authorization server encountered an unexpected
15361536+ condition that prevented it from fulfilling the request.
15371537+ (This error code is needed because a 500 Internal Server
15381538+ Error HTTP status code cannot be returned to the client
15391539+ via an HTTP redirect.)
15401540+15411541+ temporarily_unavailable
15421542+ The authorization server is currently unable to handle
15431543+ the request due to a temporary overloading or maintenance
15441544+ of the server. (This error code is needed because a 503
15451545+ Service Unavailable HTTP status code cannot be returned
15461546+ to the client via an HTTP redirect.)
15471547+15481548+ Values for the "error" parameter MUST NOT include characters
15491549+ outside the set %x20-21 / %x23-5B / %x5D-7E.
15501550+15511551+ error_description
15521552+ OPTIONAL. Human-readable ASCII [USASCII] text providing
15531553+ additional information, used to assist the client developer in
15541554+ understanding the error that occurred.
15551555+ Values for the "error_description" parameter MUST NOT include
15561556+ characters outside the set %x20-21 / %x23-5B / %x5D-7E.
15571557+15581558+ error_uri
15591559+ OPTIONAL. A URI identifying a human-readable web page with
15601560+ information about the error, used to provide the client
15611561+ developer with additional information about the error.
15621562+ Values for the "error_uri" parameter MUST conform to the
15631563+ URI-reference syntax and thus MUST NOT include characters
15641564+ outside the set %x21 / %x23-5B / %x5D-7E.
15651565+15661566+15671567+15681568+15691569+15701570+Hardt Standards Track [Page 28]
15711571+15721572+RFC 6749 OAuth 2.0 October 2012
15731573+15741574+15751575+ state
15761576+ REQUIRED if a "state" parameter was present in the client
15771577+ authorization request. The exact value received from the
15781578+ client.
15791579+15801580+ For example, the authorization server redirects the user-agent by
15811581+ sending the following HTTP response:
15821582+15831583+ HTTP/1.1 302 Found
15841584+ Location: https://client.example.com/cb?error=access_denied&state=xyz
15851585+15861586+4.1.3. Access Token Request
15871587+15881588+ The client makes a request to the token endpoint by sending the
15891589+ following parameters using the "application/x-www-form-urlencoded"
15901590+ format per Appendix B with a character encoding of UTF-8 in the HTTP
15911591+ request entity-body:
15921592+15931593+ grant_type
15941594+ REQUIRED. Value MUST be set to "authorization_code".
15951595+15961596+ code
15971597+ REQUIRED. The authorization code received from the
15981598+ authorization server.
15991599+16001600+ redirect_uri
16011601+ REQUIRED, if the "redirect_uri" parameter was included in the
16021602+ authorization request as described in Section 4.1.1, and their
16031603+ values MUST be identical.
16041604+16051605+ client_id
16061606+ REQUIRED, if the client is not authenticating with the
16071607+ authorization server as described in Section 3.2.1.
16081608+16091609+ If the client type is confidential or the client was issued client
16101610+ credentials (or assigned other authentication requirements), the
16111611+ client MUST authenticate with the authorization server as described
16121612+ in Section 3.2.1.
16131613+16141614+16151615+16161616+16171617+16181618+16191619+16201620+16211621+16221622+16231623+16241624+16251625+16261626+Hardt Standards Track [Page 29]
16271627+16281628+RFC 6749 OAuth 2.0 October 2012
16291629+16301630+16311631+ For example, the client makes the following HTTP request using TLS
16321632+ (with extra line breaks for display purposes only):
16331633+16341634+ POST /token HTTP/1.1
16351635+ Host: server.example.com
16361636+ Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
16371637+ Content-Type: application/x-www-form-urlencoded
16381638+16391639+ grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
16401640+ &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
16411641+16421642+ The authorization server MUST:
16431643+16441644+ o require client authentication for confidential clients or for any
16451645+ client that was issued client credentials (or with other
16461646+ authentication requirements),
16471647+16481648+ o authenticate the client if client authentication is included,
16491649+16501650+ o ensure that the authorization code was issued to the authenticated
16511651+ confidential client, or if the client is public, ensure that the
16521652+ code was issued to "client_id" in the request,
16531653+16541654+ o verify that the authorization code is valid, and
16551655+16561656+ o ensure that the "redirect_uri" parameter is present if the
16571657+ "redirect_uri" parameter was included in the initial authorization
16581658+ request as described in Section 4.1.1, and if included ensure that
16591659+ their values are identical.
16601660+16611661+4.1.4. Access Token Response
16621662+16631663+ If the access token request is valid and authorized, the
16641664+ authorization server issues an access token and optional refresh
16651665+ token as described in Section 5.1. If the request client
16661666+ authentication failed or is invalid, the authorization server returns
16671667+ an error response as described in Section 5.2.
16681668+16691669+16701670+16711671+16721672+16731673+16741674+16751675+16761676+16771677+16781678+16791679+16801680+16811681+16821682+Hardt Standards Track [Page 30]
16831683+16841684+RFC 6749 OAuth 2.0 October 2012
16851685+16861686+16871687+ An example successful response:
16881688+16891689+ HTTP/1.1 200 OK
16901690+ Content-Type: application/json;charset=UTF-8
16911691+ Cache-Control: no-store
16921692+ Pragma: no-cache
16931693+16941694+ {
16951695+ "access_token":"2YotnFZFEjr1zCsicMWpAA",
16961696+ "token_type":"example",
16971697+ "expires_in":3600,
16981698+ "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
16991699+ "example_parameter":"example_value"
17001700+ }
17011701+17021702+4.2. Implicit Grant
17031703+17041704+ The implicit grant type is used to obtain access tokens (it does not
17051705+ support the issuance of refresh tokens) and is optimized for public
17061706+ clients known to operate a particular redirection URI. These clients
17071707+ are typically implemented in a browser using a scripting language
17081708+ such as JavaScript.
17091709+17101710+ Since this is a redirection-based flow, the client must be capable of
17111711+ interacting with the resource owner's user-agent (typically a web
17121712+ browser) and capable of receiving incoming requests (via redirection)
17131713+ from the authorization server.
17141714+17151715+ Unlike the authorization code grant type, in which the client makes
17161716+ separate requests for authorization and for an access token, the
17171717+ client receives the access token as the result of the authorization
17181718+ request.
17191719+17201720+ The implicit grant type does not include client authentication, and
17211721+ relies on the presence of the resource owner and the registration of
17221722+ the redirection URI. Because the access token is encoded into the
17231723+ redirection URI, it may be exposed to the resource owner and other
17241724+ applications residing on the same device.
17251725+17261726+17271727+17281728+17291729+17301730+17311731+17321732+17331733+17341734+17351735+17361736+17371737+17381738+Hardt Standards Track [Page 31]
17391739+17401740+RFC 6749 OAuth 2.0 October 2012
17411741+17421742+17431743+ +----------+
17441744+ | Resource |
17451745+ | Owner |
17461746+ | |
17471747+ +----------+
17481748+ ^
17491749+ |
17501750+ (B)
17511751+ +----|-----+ Client Identifier +---------------+
17521752+ | -+----(A)-- & Redirection URI --->| |
17531753+ | User- | | Authorization |
17541754+ | Agent -|----(B)-- User authenticates -->| Server |
17551755+ | | | |
17561756+ | |<---(C)--- Redirection URI ----<| |
17571757+ | | with Access Token +---------------+
17581758+ | | in Fragment
17591759+ | | +---------------+
17601760+ | |----(D)--- Redirection URI ---->| Web-Hosted |
17611761+ | | without Fragment | Client |
17621762+ | | | Resource |
17631763+ | (F) |<---(E)------- Script ---------<| |
17641764+ | | +---------------+
17651765+ +-|--------+
17661766+ | |
17671767+ (A) (G) Access Token
17681768+ | |
17691769+ ^ v
17701770+ +---------+
17711771+ | |
17721772+ | Client |
17731773+ | |
17741774+ +---------+
17751775+17761776+ Note: The lines illustrating steps (A) and (B) are broken into two
17771777+ parts as they pass through the user-agent.
17781778+17791779+ Figure 4: Implicit Grant Flow
17801780+17811781+17821782+17831783+17841784+17851785+17861786+17871787+17881788+17891789+17901790+17911791+17921792+17931793+17941794+Hardt Standards Track [Page 32]
17951795+17961796+RFC 6749 OAuth 2.0 October 2012
17971797+17981798+17991799+ The flow illustrated in Figure 4 includes the following steps:
18001800+18011801+ (A) The client initiates the flow by directing the resource owner's
18021802+ user-agent to the authorization endpoint. The client includes
18031803+ its client identifier, requested scope, local state, and a
18041804+ redirection URI to which the authorization server will send the
18051805+ user-agent back once access is granted (or denied).
18061806+18071807+ (B) The authorization server authenticates the resource owner (via
18081808+ the user-agent) and establishes whether the resource owner
18091809+ grants or denies the client's access request.
18101810+18111811+ (C) Assuming the resource owner grants access, the authorization
18121812+ server redirects the user-agent back to the client using the
18131813+ redirection URI provided earlier. The redirection URI includes
18141814+ the access token in the URI fragment.
18151815+18161816+ (D) The user-agent follows the redirection instructions by making a
18171817+ request to the web-hosted client resource (which does not
18181818+ include the fragment per [RFC2616]). The user-agent retains the
18191819+ fragment information locally.
18201820+18211821+ (E) The web-hosted client resource returns a web page (typically an
18221822+ HTML document with an embedded script) capable of accessing the
18231823+ full redirection URI including the fragment retained by the
18241824+ user-agent, and extracting the access token (and other
18251825+ parameters) contained in the fragment.
18261826+18271827+ (F) The user-agent executes the script provided by the web-hosted
18281828+ client resource locally, which extracts the access token.
18291829+18301830+ (G) The user-agent passes the access token to the client.
18311831+18321832+ See Sections 1.3.2 and 9 for background on using the implicit grant.
18331833+ See Sections 10.3 and 10.16 for important security considerations
18341834+ when using the implicit grant.
18351835+18361836+4.2.1. Authorization Request
18371837+18381838+ The client constructs the request URI by adding the following
18391839+ parameters to the query component of the authorization endpoint URI
18401840+ using the "application/x-www-form-urlencoded" format, per Appendix B:
18411841+18421842+ response_type
18431843+ REQUIRED. Value MUST be set to "token".
18441844+18451845+ client_id
18461846+ REQUIRED. The client identifier as described in Section 2.2.
18471847+18481848+18491849+18501850+Hardt Standards Track [Page 33]
18511851+18521852+RFC 6749 OAuth 2.0 October 2012
18531853+18541854+18551855+ redirect_uri
18561856+ OPTIONAL. As described in Section 3.1.2.
18571857+18581858+ scope
18591859+ OPTIONAL. The scope of the access request as described by
18601860+ Section 3.3.
18611861+18621862+ state
18631863+ RECOMMENDED. An opaque value used by the client to maintain
18641864+ state between the request and callback. The authorization
18651865+ server includes this value when redirecting the user-agent back
18661866+ to the client. The parameter SHOULD be used for preventing
18671867+ cross-site request forgery as described in Section 10.12.
18681868+18691869+ The client directs the resource owner to the constructed URI using an
18701870+ HTTP redirection response, or by other means available to it via the
18711871+ user-agent.
18721872+18731873+ For example, the client directs the user-agent to make the following
18741874+ HTTP request using TLS (with extra line breaks for display purposes
18751875+ only):
18761876+18771877+ GET /authorize?response_type=token&client_id=s6BhdRkqt3&state=xyz
18781878+ &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
18791879+ Host: server.example.com
18801880+18811881+ The authorization server validates the request to ensure that all
18821882+ required parameters are present and valid. The authorization server
18831883+ MUST verify that the redirection URI to which it will redirect the
18841884+ access token matches a redirection URI registered by the client as
18851885+ described in Section 3.1.2.
18861886+18871887+ If the request is valid, the authorization server authenticates the
18881888+ resource owner and obtains an authorization decision (by asking the
18891889+ resource owner or by establishing approval via other means).
18901890+18911891+ When a decision is established, the authorization server directs the
18921892+ user-agent to the provided client redirection URI using an HTTP
18931893+ redirection response, or by other means available to it via the
18941894+ user-agent.
18951895+18961896+18971897+18981898+18991899+19001900+19011901+19021902+19031903+19041904+19051905+19061906+Hardt Standards Track [Page 34]
19071907+19081908+RFC 6749 OAuth 2.0 October 2012
19091909+19101910+19111911+4.2.2. Access Token Response
19121912+19131913+ If the resource owner grants the access request, the authorization
19141914+ server issues an access token and delivers it to the client by adding
19151915+ the following parameters to the fragment component of the redirection
19161916+ URI using the "application/x-www-form-urlencoded" format, per
19171917+ Appendix B:
19181918+19191919+ access_token
19201920+ REQUIRED. The access token issued by the authorization server.
19211921+19221922+ token_type
19231923+ REQUIRED. The type of the token issued as described in
19241924+ Section 7.1. Value is case insensitive.
19251925+19261926+ expires_in
19271927+ RECOMMENDED. The lifetime in seconds of the access token. For
19281928+ example, the value "3600" denotes that the access token will
19291929+ expire in one hour from the time the response was generated.
19301930+ If omitted, the authorization server SHOULD provide the
19311931+ expiration time via other means or document the default value.
19321932+19331933+ scope
19341934+ OPTIONAL, if identical to the scope requested by the client;
19351935+ otherwise, REQUIRED. The scope of the access token as
19361936+ described by Section 3.3.
19371937+19381938+ state
19391939+ REQUIRED if the "state" parameter was present in the client
19401940+ authorization request. The exact value received from the
19411941+ client.
19421942+19431943+ The authorization server MUST NOT issue a refresh token.
19441944+19451945+ For example, the authorization server redirects the user-agent by
19461946+ sending the following HTTP response (with extra line breaks for
19471947+ display purposes only):
19481948+19491949+ HTTP/1.1 302 Found
19501950+ Location: http://example.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA
19511951+ &state=xyz&token_type=example&expires_in=3600
19521952+19531953+ Developers should note that some user-agents do not support the
19541954+ inclusion of a fragment component in the HTTP "Location" response
19551955+ header field. Such clients will require using other methods for
19561956+ redirecting the client than a 3xx redirection response -- for
19571957+ example, returning an HTML page that includes a 'continue' button
19581958+ with an action linked to the redirection URI.
19591959+19601960+19611961+19621962+Hardt Standards Track [Page 35]
19631963+19641964+RFC 6749 OAuth 2.0 October 2012
19651965+19661966+19671967+ The client MUST ignore unrecognized response parameters. The access
19681968+ token string size is left undefined by this specification. The
19691969+ client should avoid making assumptions about value sizes. The
19701970+ authorization server SHOULD document the size of any value it issues.
19711971+19721972+4.2.2.1. Error Response
19731973+19741974+ If the request fails due to a missing, invalid, or mismatching
19751975+ redirection URI, or if the client identifier is missing or invalid,
19761976+ the authorization server SHOULD inform the resource owner of the
19771977+ error and MUST NOT automatically redirect the user-agent to the
19781978+ invalid redirection URI.
19791979+19801980+ If the resource owner denies the access request or if the request
19811981+ fails for reasons other than a missing or invalid redirection URI,
19821982+ the authorization server informs the client by adding the following
19831983+ parameters to the fragment component of the redirection URI using the
19841984+ "application/x-www-form-urlencoded" format, per Appendix B:
19851985+19861986+ error
19871987+ REQUIRED. A single ASCII [USASCII] error code from the
19881988+ following:
19891989+19901990+ invalid_request
19911991+ The request is missing a required parameter, includes an
19921992+ invalid parameter value, includes a parameter more than
19931993+ once, or is otherwise malformed.
19941994+19951995+ unauthorized_client
19961996+ The client is not authorized to request an access token
19971997+ using this method.
19981998+19991999+ access_denied
20002000+ The resource owner or authorization server denied the
20012001+ request.
20022002+20032003+ unsupported_response_type
20042004+ The authorization server does not support obtaining an
20052005+ access token using this method.
20062006+20072007+ invalid_scope
20082008+ The requested scope is invalid, unknown, or malformed.
20092009+20102010+20112011+20122012+20132013+20142014+20152015+20162016+20172017+20182018+Hardt Standards Track [Page 36]
20192019+20202020+RFC 6749 OAuth 2.0 October 2012
20212021+20222022+20232023+ server_error
20242024+ The authorization server encountered an unexpected
20252025+ condition that prevented it from fulfilling the request.
20262026+ (This error code is needed because a 500 Internal Server
20272027+ Error HTTP status code cannot be returned to the client
20282028+ via an HTTP redirect.)
20292029+20302030+ temporarily_unavailable
20312031+ The authorization server is currently unable to handle
20322032+ the request due to a temporary overloading or maintenance
20332033+ of the server. (This error code is needed because a 503
20342034+ Service Unavailable HTTP status code cannot be returned
20352035+ to the client via an HTTP redirect.)
20362036+20372037+ Values for the "error" parameter MUST NOT include characters
20382038+ outside the set %x20-21 / %x23-5B / %x5D-7E.
20392039+20402040+ error_description
20412041+ OPTIONAL. Human-readable ASCII [USASCII] text providing
20422042+ additional information, used to assist the client developer in
20432043+ understanding the error that occurred.
20442044+ Values for the "error_description" parameter MUST NOT include
20452045+ characters outside the set %x20-21 / %x23-5B / %x5D-7E.
20462046+20472047+ error_uri
20482048+ OPTIONAL. A URI identifying a human-readable web page with
20492049+ information about the error, used to provide the client
20502050+ developer with additional information about the error.
20512051+ Values for the "error_uri" parameter MUST conform to the
20522052+ URI-reference syntax and thus MUST NOT include characters
20532053+ outside the set %x21 / %x23-5B / %x5D-7E.
20542054+20552055+ state
20562056+ REQUIRED if a "state" parameter was present in the client
20572057+ authorization request. The exact value received from the
20582058+ client.
20592059+20602060+ For example, the authorization server redirects the user-agent by
20612061+ sending the following HTTP response:
20622062+20632063+ HTTP/1.1 302 Found
20642064+ Location: https://client.example.com/cb#error=access_denied&state=xyz
20652065+20662066+4.3. Resource Owner Password Credentials Grant
20672067+20682068+ The resource owner password credentials grant type is suitable in
20692069+ cases where the resource owner has a trust relationship with the
20702070+ client, such as the device operating system or a highly privileged
20712071+20722072+20732073+20742074+Hardt Standards Track [Page 37]
20752075+20762076+RFC 6749 OAuth 2.0 October 2012
20772077+20782078+20792079+ application. The authorization server should take special care when
20802080+ enabling this grant type and only allow it when other flows are not
20812081+ viable.
20822082+20832083+ This grant type is suitable for clients capable of obtaining the
20842084+ resource owner's credentials (username and password, typically using
20852085+ an interactive form). It is also used to migrate existing clients
20862086+ using direct authentication schemes such as HTTP Basic or Digest
20872087+ authentication to OAuth by converting the stored credentials to an
20882088+ access token.
20892089+20902090+ +----------+
20912091+ | Resource |
20922092+ | Owner |
20932093+ | |
20942094+ +----------+
20952095+ v
20962096+ | Resource Owner
20972097+ (A) Password Credentials
20982098+ |
20992099+ v
21002100+ +---------+ +---------------+
21012101+ | |>--(B)---- Resource Owner ------->| |
21022102+ | | Password Credentials | Authorization |
21032103+ | Client | | Server |
21042104+ | |<--(C)---- Access Token ---------<| |
21052105+ | | (w/ Optional Refresh Token) | |
21062106+ +---------+ +---------------+
21072107+21082108+ Figure 5: Resource Owner Password Credentials Flow
21092109+21102110+ The flow illustrated in Figure 5 includes the following steps:
21112111+21122112+ (A) The resource owner provides the client with its username and
21132113+ password.
21142114+21152115+ (B) The client requests an access token from the authorization
21162116+ server's token endpoint by including the credentials received
21172117+ from the resource owner. When making the request, the client
21182118+ authenticates with the authorization server.
21192119+21202120+ (C) The authorization server authenticates the client and validates
21212121+ the resource owner credentials, and if valid, issues an access
21222122+ token.
21232123+21242124+21252125+21262126+21272127+21282128+21292129+21302130+Hardt Standards Track [Page 38]
21312131+21322132+RFC 6749 OAuth 2.0 October 2012
21332133+21342134+21352135+4.3.1. Authorization Request and Response
21362136+21372137+ The method through which the client obtains the resource owner
21382138+ credentials is beyond the scope of this specification. The client
21392139+ MUST discard the credentials once an access token has been obtained.
21402140+21412141+4.3.2. Access Token Request
21422142+21432143+ The client makes a request to the token endpoint by adding the
21442144+ following parameters using the "application/x-www-form-urlencoded"
21452145+ format per Appendix B with a character encoding of UTF-8 in the HTTP
21462146+ request entity-body:
21472147+21482148+ grant_type
21492149+ REQUIRED. Value MUST be set to "password".
21502150+21512151+ username
21522152+ REQUIRED. The resource owner username.
21532153+21542154+ password
21552155+ REQUIRED. The resource owner password.
21562156+21572157+ scope
21582158+ OPTIONAL. The scope of the access request as described by
21592159+ Section 3.3.
21602160+21612161+ If the client type is confidential or the client was issued client
21622162+ credentials (or assigned other authentication requirements), the
21632163+ client MUST authenticate with the authorization server as described
21642164+ in Section 3.2.1.
21652165+21662166+ For example, the client makes the following HTTP request using
21672167+ transport-layer security (with extra line breaks for display purposes
21682168+ only):
21692169+21702170+ POST /token HTTP/1.1
21712171+ Host: server.example.com
21722172+ Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
21732173+ Content-Type: application/x-www-form-urlencoded
21742174+21752175+ grant_type=password&username=johndoe&password=A3ddj3w
21762176+21772177+21782178+21792179+21802180+21812181+21822182+21832183+21842184+21852185+21862186+Hardt Standards Track [Page 39]
21872187+21882188+RFC 6749 OAuth 2.0 October 2012
21892189+21902190+21912191+ The authorization server MUST:
21922192+21932193+ o require client authentication for confidential clients or for any
21942194+ client that was issued client credentials (or with other
21952195+ authentication requirements),
21962196+21972197+ o authenticate the client if client authentication is included, and
21982198+21992199+ o validate the resource owner password credentials using its
22002200+ existing password validation algorithm.
22012201+22022202+ Since this access token request utilizes the resource owner's
22032203+ password, the authorization server MUST protect the endpoint against
22042204+ brute force attacks (e.g., using rate-limitation or generating
22052205+ alerts).
22062206+22072207+4.3.3. Access Token Response
22082208+22092209+ If the access token request is valid and authorized, the
22102210+ authorization server issues an access token and optional refresh
22112211+ token as described in Section 5.1. If the request failed client
22122212+ authentication or is invalid, the authorization server returns an
22132213+ error response as described in Section 5.2.
22142214+22152215+ An example successful response:
22162216+22172217+ HTTP/1.1 200 OK
22182218+ Content-Type: application/json;charset=UTF-8
22192219+ Cache-Control: no-store
22202220+ Pragma: no-cache
22212221+22222222+ {
22232223+ "access_token":"2YotnFZFEjr1zCsicMWpAA",
22242224+ "token_type":"example",
22252225+ "expires_in":3600,
22262226+ "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
22272227+ "example_parameter":"example_value"
22282228+ }
22292229+22302230+4.4. Client Credentials Grant
22312231+22322232+ The client can request an access token using only its client
22332233+ credentials (or other supported means of authentication) when the
22342234+ client is requesting access to the protected resources under its
22352235+ control, or those of another resource owner that have been previously
22362236+ arranged with the authorization server (the method of which is beyond
22372237+ the scope of this specification).
22382238+22392239+22402240+22412241+22422242+Hardt Standards Track [Page 40]
22432243+22442244+RFC 6749 OAuth 2.0 October 2012
22452245+22462246+22472247+ The client credentials grant type MUST only be used by confidential
22482248+ clients.
22492249+22502250+ +---------+ +---------------+
22512251+ | | | |
22522252+ | |>--(A)- Client Authentication --->| Authorization |
22532253+ | Client | | Server |
22542254+ | |<--(B)---- Access Token ---------<| |
22552255+ | | | |
22562256+ +---------+ +---------------+
22572257+22582258+ Figure 6: Client Credentials Flow
22592259+22602260+ The flow illustrated in Figure 6 includes the following steps:
22612261+22622262+ (A) The client authenticates with the authorization server and
22632263+ requests an access token from the token endpoint.
22642264+22652265+ (B) The authorization server authenticates the client, and if valid,
22662266+ issues an access token.
22672267+22682268+4.4.1. Authorization Request and Response
22692269+22702270+ Since the client authentication is used as the authorization grant,
22712271+ no additional authorization request is needed.
22722272+22732273+4.4.2. Access Token Request
22742274+22752275+ The client makes a request to the token endpoint by adding the
22762276+ following parameters using the "application/x-www-form-urlencoded"
22772277+ format per Appendix B with a character encoding of UTF-8 in the HTTP
22782278+ request entity-body:
22792279+22802280+ grant_type
22812281+ REQUIRED. Value MUST be set to "client_credentials".
22822282+22832283+ scope
22842284+ OPTIONAL. The scope of the access request as described by
22852285+ Section 3.3.
22862286+22872287+ The client MUST authenticate with the authorization server as
22882288+ described in Section 3.2.1.
22892289+22902290+22912291+22922292+22932293+22942294+22952295+22962296+22972297+22982298+Hardt Standards Track [Page 41]
22992299+23002300+RFC 6749 OAuth 2.0 October 2012
23012301+23022302+23032303+ For example, the client makes the following HTTP request using
23042304+ transport-layer security (with extra line breaks for display purposes
23052305+ only):
23062306+23072307+ POST /token HTTP/1.1
23082308+ Host: server.example.com
23092309+ Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
23102310+ Content-Type: application/x-www-form-urlencoded
23112311+23122312+ grant_type=client_credentials
23132313+23142314+ The authorization server MUST authenticate the client.
23152315+23162316+4.4.3. Access Token Response
23172317+23182318+ If the access token request is valid and authorized, the
23192319+ authorization server issues an access token as described in
23202320+ Section 5.1. A refresh token SHOULD NOT be included. If the request
23212321+ failed client authentication or is invalid, the authorization server
23222322+ returns an error response as described in Section 5.2.
23232323+23242324+ An example successful response:
23252325+23262326+ HTTP/1.1 200 OK
23272327+ Content-Type: application/json;charset=UTF-8
23282328+ Cache-Control: no-store
23292329+ Pragma: no-cache
23302330+23312331+ {
23322332+ "access_token":"2YotnFZFEjr1zCsicMWpAA",
23332333+ "token_type":"example",
23342334+ "expires_in":3600,
23352335+ "example_parameter":"example_value"
23362336+ }
23372337+23382338+4.5. Extension Grants
23392339+23402340+ The client uses an extension grant type by specifying the grant type
23412341+ using an absolute URI (defined by the authorization server) as the
23422342+ value of the "grant_type" parameter of the token endpoint, and by
23432343+ adding any additional parameters necessary.
23442344+23452345+23462346+23472347+23482348+23492349+23502350+23512351+23522352+23532353+23542354+Hardt Standards Track [Page 42]
23552355+23562356+RFC 6749 OAuth 2.0 October 2012
23572357+23582358+23592359+ For example, to request an access token using a Security Assertion
23602360+ Markup Language (SAML) 2.0 assertion grant type as defined by
23612361+ [OAuth-SAML2], the client could make the following HTTP request using
23622362+ TLS (with extra line breaks for display purposes only):
23632363+23642364+ POST /token HTTP/1.1
23652365+ Host: server.example.com
23662366+ Content-Type: application/x-www-form-urlencoded
23672367+23682368+ grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Asaml2-
23692369+ bearer&assertion=PEFzc2VydGlvbiBJc3N1ZUluc3RhbnQ9IjIwMTEtMDU
23702370+ [...omitted for brevity...]aG5TdGF0ZW1lbnQ-PC9Bc3NlcnRpb24-
23712371+23722372+ If the access token request is valid and authorized, the
23732373+ authorization server issues an access token and optional refresh
23742374+ token as described in Section 5.1. If the request failed client
23752375+ authentication or is invalid, the authorization server returns an
23762376+ error response as described in Section 5.2.
23772377+23782378+5. Issuing an Access Token
23792379+23802380+ If the access token request is valid and authorized, the
23812381+ authorization server issues an access token and optional refresh
23822382+ token as described in Section 5.1. If the request failed client
23832383+ authentication or is invalid, the authorization server returns an
23842384+ error response as described in Section 5.2.
23852385+23862386+5.1. Successful Response
23872387+23882388+ The authorization server issues an access token and optional refresh
23892389+ token, and constructs the response by adding the following parameters
23902390+ to the entity-body of the HTTP response with a 200 (OK) status code:
23912391+23922392+ access_token
23932393+ REQUIRED. The access token issued by the authorization server.
23942394+23952395+ token_type
23962396+ REQUIRED. The type of the token issued as described in
23972397+ Section 7.1. Value is case insensitive.
23982398+23992399+ expires_in
24002400+ RECOMMENDED. The lifetime in seconds of the access token. For
24012401+ example, the value "3600" denotes that the access token will
24022402+ expire in one hour from the time the response was generated.
24032403+ If omitted, the authorization server SHOULD provide the
24042404+ expiration time via other means or document the default value.
24052405+24062406+24072407+24082408+24092409+24102410+Hardt Standards Track [Page 43]
24112411+24122412+RFC 6749 OAuth 2.0 October 2012
24132413+24142414+24152415+ refresh_token
24162416+ OPTIONAL. The refresh token, which can be used to obtain new
24172417+ access tokens using the same authorization grant as described
24182418+ in Section 6.
24192419+24202420+ scope
24212421+ OPTIONAL, if identical to the scope requested by the client;
24222422+ otherwise, REQUIRED. The scope of the access token as
24232423+ described by Section 3.3.
24242424+24252425+ The parameters are included in the entity-body of the HTTP response
24262426+ using the "application/json" media type as defined by [RFC4627]. The
24272427+ parameters are serialized into a JavaScript Object Notation (JSON)
24282428+ structure by adding each parameter at the highest structure level.
24292429+ Parameter names and string values are included as JSON strings.
24302430+ Numerical values are included as JSON numbers. The order of
24312431+ parameters does not matter and can vary.
24322432+24332433+ The authorization server MUST include the HTTP "Cache-Control"
24342434+ response header field [RFC2616] with a value of "no-store" in any
24352435+ response containing tokens, credentials, or other sensitive
24362436+ information, as well as the "Pragma" response header field [RFC2616]
24372437+ with a value of "no-cache".
24382438+24392439+ For example:
24402440+24412441+ HTTP/1.1 200 OK
24422442+ Content-Type: application/json;charset=UTF-8
24432443+ Cache-Control: no-store
24442444+ Pragma: no-cache
24452445+24462446+ {
24472447+ "access_token":"2YotnFZFEjr1zCsicMWpAA",
24482448+ "token_type":"example",
24492449+ "expires_in":3600,
24502450+ "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
24512451+ "example_parameter":"example_value"
24522452+ }
24532453+24542454+ The client MUST ignore unrecognized value names in the response. The
24552455+ sizes of tokens and other values received from the authorization
24562456+ server are left undefined. The client should avoid making
24572457+ assumptions about value sizes. The authorization server SHOULD
24582458+ document the size of any value it issues.
24592459+24602460+24612461+24622462+24632463+24642464+24652465+24662466+Hardt Standards Track [Page 44]
24672467+24682468+RFC 6749 OAuth 2.0 October 2012
24692469+24702470+24712471+5.2. Error Response
24722472+24732473+ The authorization server responds with an HTTP 400 (Bad Request)
24742474+ status code (unless specified otherwise) and includes the following
24752475+ parameters with the response:
24762476+24772477+ error
24782478+ REQUIRED. A single ASCII [USASCII] error code from the
24792479+ following:
24802480+24812481+ invalid_request
24822482+ The request is missing a required parameter, includes an
24832483+ unsupported parameter value (other than grant type),
24842484+ repeats a parameter, includes multiple credentials,
24852485+ utilizes more than one mechanism for authenticating the
24862486+ client, or is otherwise malformed.
24872487+24882488+ invalid_client
24892489+ Client authentication failed (e.g., unknown client, no
24902490+ client authentication included, or unsupported
24912491+ authentication method). The authorization server MAY
24922492+ return an HTTP 401 (Unauthorized) status code to indicate
24932493+ which HTTP authentication schemes are supported. If the
24942494+ client attempted to authenticate via the "Authorization"
24952495+ request header field, the authorization server MUST
24962496+ respond with an HTTP 401 (Unauthorized) status code and
24972497+ include the "WWW-Authenticate" response header field
24982498+ matching the authentication scheme used by the client.
24992499+25002500+ invalid_grant
25012501+ The provided authorization grant (e.g., authorization
25022502+ code, resource owner credentials) or refresh token is
25032503+ invalid, expired, revoked, does not match the redirection
25042504+ URI used in the authorization request, or was issued to
25052505+ another client.
25062506+25072507+ unauthorized_client
25082508+ The authenticated client is not authorized to use this
25092509+ authorization grant type.
25102510+25112511+ unsupported_grant_type
25122512+ The authorization grant type is not supported by the
25132513+ authorization server.
25142514+25152515+25162516+25172517+25182518+25192519+25202520+25212521+25222522+Hardt Standards Track [Page 45]
25232523+25242524+RFC 6749 OAuth 2.0 October 2012
25252525+25262526+25272527+ invalid_scope
25282528+ The requested scope is invalid, unknown, malformed, or
25292529+ exceeds the scope granted by the resource owner.
25302530+25312531+ Values for the "error" parameter MUST NOT include characters
25322532+ outside the set %x20-21 / %x23-5B / %x5D-7E.
25332533+25342534+ error_description
25352535+ OPTIONAL. Human-readable ASCII [USASCII] text providing
25362536+ additional information, used to assist the client developer in
25372537+ understanding the error that occurred.
25382538+ Values for the "error_description" parameter MUST NOT include
25392539+ characters outside the set %x20-21 / %x23-5B / %x5D-7E.
25402540+25412541+ error_uri
25422542+ OPTIONAL. A URI identifying a human-readable web page with
25432543+ information about the error, used to provide the client
25442544+ developer with additional information about the error.
25452545+ Values for the "error_uri" parameter MUST conform to the
25462546+ URI-reference syntax and thus MUST NOT include characters
25472547+ outside the set %x21 / %x23-5B / %x5D-7E.
25482548+25492549+ The parameters are included in the entity-body of the HTTP response
25502550+ using the "application/json" media type as defined by [RFC4627]. The
25512551+ parameters are serialized into a JSON structure by adding each
25522552+ parameter at the highest structure level. Parameter names and string
25532553+ values are included as JSON strings. Numerical values are included
25542554+ as JSON numbers. The order of parameters does not matter and can
25552555+ vary.
25562556+25572557+ For example:
25582558+25592559+ HTTP/1.1 400 Bad Request
25602560+ Content-Type: application/json;charset=UTF-8
25612561+ Cache-Control: no-store
25622562+ Pragma: no-cache
25632563+25642564+ {
25652565+ "error":"invalid_request"
25662566+ }
25672567+25682568+25692569+25702570+25712571+25722572+25732573+25742574+25752575+25762576+25772577+25782578+Hardt Standards Track [Page 46]
25792579+25802580+RFC 6749 OAuth 2.0 October 2012
25812581+25822582+25832583+6. Refreshing an Access Token
25842584+25852585+ If the authorization server issued a refresh token to the client, the
25862586+ client makes a refresh request to the token endpoint by adding the
25872587+ following parameters using the "application/x-www-form-urlencoded"
25882588+ format per Appendix B with a character encoding of UTF-8 in the HTTP
25892589+ request entity-body:
25902590+25912591+ grant_type
25922592+ REQUIRED. Value MUST be set to "refresh_token".
25932593+25942594+ refresh_token
25952595+ REQUIRED. The refresh token issued to the client.
25962596+25972597+ scope
25982598+ OPTIONAL. The scope of the access request as described by
25992599+ Section 3.3. The requested scope MUST NOT include any scope
26002600+ not originally granted by the resource owner, and if omitted is
26012601+ treated as equal to the scope originally granted by the
26022602+ resource owner.
26032603+26042604+ Because refresh tokens are typically long-lasting credentials used to
26052605+ request additional access tokens, the refresh token is bound to the
26062606+ client to which it was issued. If the client type is confidential or
26072607+ the client was issued client credentials (or assigned other
26082608+ authentication requirements), the client MUST authenticate with the
26092609+ authorization server as described in Section 3.2.1.
26102610+26112611+ For example, the client makes the following HTTP request using
26122612+ transport-layer security (with extra line breaks for display purposes
26132613+ only):
26142614+26152615+ POST /token HTTP/1.1
26162616+ Host: server.example.com
26172617+ Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
26182618+ Content-Type: application/x-www-form-urlencoded
26192619+26202620+ grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
26212621+26222622+26232623+26242624+26252625+26262626+26272627+26282628+26292629+26302630+26312631+26322632+26332633+26342634+Hardt Standards Track [Page 47]
26352635+26362636+RFC 6749 OAuth 2.0 October 2012
26372637+26382638+26392639+ The authorization server MUST:
26402640+26412641+ o require client authentication for confidential clients or for any
26422642+ client that was issued client credentials (or with other
26432643+ authentication requirements),
26442644+26452645+ o authenticate the client if client authentication is included and
26462646+ ensure that the refresh token was issued to the authenticated
26472647+ client, and
26482648+26492649+ o validate the refresh token.
26502650+26512651+ If valid and authorized, the authorization server issues an access
26522652+ token as described in Section 5.1. If the request failed
26532653+ verification or is invalid, the authorization server returns an error
26542654+ response as described in Section 5.2.
26552655+26562656+ The authorization server MAY issue a new refresh token, in which case
26572657+ the client MUST discard the old refresh token and replace it with the
26582658+ new refresh token. The authorization server MAY revoke the old
26592659+ refresh token after issuing a new refresh token to the client. If a
26602660+ new refresh token is issued, the refresh token scope MUST be
26612661+ identical to that of the refresh token included by the client in the
26622662+ request.
26632663+26642664+7. Accessing Protected Resources
26652665+26662666+ The client accesses protected resources by presenting the access
26672667+ token to the resource server. The resource server MUST validate the
26682668+ access token and ensure that it has not expired and that its scope
26692669+ covers the requested resource. The methods used by the resource
26702670+ server to validate the access token (as well as any error responses)
26712671+ are beyond the scope of this specification but generally involve an
26722672+ interaction or coordination between the resource server and the
26732673+ authorization server.
26742674+26752675+ The method in which the client utilizes the access token to
26762676+ authenticate with the resource server depends on the type of access
26772677+ token issued by the authorization server. Typically, it involves
26782678+ using the HTTP "Authorization" request header field [RFC2617] with an
26792679+ authentication scheme defined by the specification of the access
26802680+ token type used, such as [RFC6750].
26812681+26822682+26832683+26842684+26852685+26862686+26872687+26882688+26892689+26902690+Hardt Standards Track [Page 48]
26912691+26922692+RFC 6749 OAuth 2.0 October 2012
26932693+26942694+26952695+7.1. Access Token Types
26962696+26972697+ The access token type provides the client with the information
26982698+ required to successfully utilize the access token to make a protected
26992699+ resource request (along with type-specific attributes). The client
27002700+ MUST NOT use an access token if it does not understand the token
27012701+ type.
27022702+27032703+ For example, the "bearer" token type defined in [RFC6750] is utilized
27042704+ by simply including the access token string in the request:
27052705+27062706+ GET /resource/1 HTTP/1.1
27072707+ Host: example.com
27082708+ Authorization: Bearer mF_9.B5f-4.1JqM
27092709+27102710+ while the "mac" token type defined in [OAuth-HTTP-MAC] is utilized by
27112711+ issuing a Message Authentication Code (MAC) key together with the
27122712+ access token that is used to sign certain components of the HTTP
27132713+ requests:
27142714+27152715+ GET /resource/1 HTTP/1.1
27162716+ Host: example.com
27172717+ Authorization: MAC id="h480djs93hd8",
27182718+ nonce="274312:dj83hs9s",
27192719+ mac="kDZvddkndxvhGRXZhvuDjEWhGeE="
27202720+27212721+ The above examples are provided for illustration purposes only.
27222722+ Developers are advised to consult the [RFC6750] and [OAuth-HTTP-MAC]
27232723+ specifications before use.
27242724+27252725+ Each access token type definition specifies the additional attributes
27262726+ (if any) sent to the client together with the "access_token" response
27272727+ parameter. It also defines the HTTP authentication method used to
27282728+ include the access token when making a protected resource request.
27292729+27302730+7.2. Error Response
27312731+27322732+ If a resource access request fails, the resource server SHOULD inform
27332733+ the client of the error. While the specifics of such error responses
27342734+ are beyond the scope of this specification, this document establishes
27352735+ a common registry in Section 11.4 for error values to be shared among
27362736+ OAuth token authentication schemes.
27372737+27382738+ New authentication schemes designed primarily for OAuth token
27392739+ authentication SHOULD define a mechanism for providing an error
27402740+ status code to the client, in which the error values allowed are
27412741+ registered in the error registry established by this specification.
27422742+27432743+27442744+27452745+27462746+Hardt Standards Track [Page 49]
27472747+27482748+RFC 6749 OAuth 2.0 October 2012
27492749+27502750+27512751+ Such schemes MAY limit the set of valid error codes to a subset of
27522752+ the registered values. If the error code is returned using a named
27532753+ parameter, the parameter name SHOULD be "error".
27542754+27552755+ Other schemes capable of being used for OAuth token authentication,
27562756+ but not primarily designed for that purpose, MAY bind their error
27572757+ values to the registry in the same manner.
27582758+27592759+ New authentication schemes MAY choose to also specify the use of the
27602760+ "error_description" and "error_uri" parameters to return error
27612761+ information in a manner parallel to their usage in this
27622762+ specification.
27632763+27642764+8. Extensibility
27652765+27662766+8.1. Defining Access Token Types
27672767+27682768+ Access token types can be defined in one of two ways: registered in
27692769+ the Access Token Types registry (following the procedures in
27702770+ Section 11.1), or by using a unique absolute URI as its name.
27712771+27722772+ Types utilizing a URI name SHOULD be limited to vendor-specific
27732773+ implementations that are not commonly applicable, and are specific to
27742774+ the implementation details of the resource server where they are
27752775+ used.
27762776+27772777+ All other types MUST be registered. Type names MUST conform to the
27782778+ type-name ABNF. If the type definition includes a new HTTP
27792779+ authentication scheme, the type name SHOULD be identical to the HTTP
27802780+ authentication scheme name (as defined by [RFC2617]). The token type
27812781+ "example" is reserved for use in examples.
27822782+27832783+ type-name = 1*name-char
27842784+ name-char = "-" / "." / "_" / DIGIT / ALPHA
27852785+27862786+8.2. Defining New Endpoint Parameters
27872787+27882788+ New request or response parameters for use with the authorization
27892789+ endpoint or the token endpoint are defined and registered in the
27902790+ OAuth Parameters registry following the procedure in Section 11.2.
27912791+27922792+ Parameter names MUST conform to the param-name ABNF, and parameter
27932793+ values syntax MUST be well-defined (e.g., using ABNF, or a reference
27942794+ to the syntax of an existing parameter).
27952795+27962796+ param-name = 1*name-char
27972797+ name-char = "-" / "." / "_" / DIGIT / ALPHA
27982798+27992799+28002800+28012801+28022802+Hardt Standards Track [Page 50]
28032803+28042804+RFC 6749 OAuth 2.0 October 2012
28052805+28062806+28072807+ Unregistered vendor-specific parameter extensions that are not
28082808+ commonly applicable and that are specific to the implementation
28092809+ details of the authorization server where they are used SHOULD
28102810+ utilize a vendor-specific prefix that is not likely to conflict with
28112811+ other registered values (e.g., begin with 'companyname_').
28122812+28132813+8.3. Defining New Authorization Grant Types
28142814+28152815+ New authorization grant types can be defined by assigning them a
28162816+ unique absolute URI for use with the "grant_type" parameter. If the
28172817+ extension grant type requires additional token endpoint parameters,
28182818+ they MUST be registered in the OAuth Parameters registry as described
28192819+ by Section 11.2.
28202820+28212821+8.4. Defining New Authorization Endpoint Response Types
28222822+28232823+ New response types for use with the authorization endpoint are
28242824+ defined and registered in the Authorization Endpoint Response Types
28252825+ registry following the procedure in Section 11.3. Response type
28262826+ names MUST conform to the response-type ABNF.
28272827+28282828+ response-type = response-name *( SP response-name )
28292829+ response-name = 1*response-char
28302830+ response-char = "_" / DIGIT / ALPHA
28312831+28322832+ If a response type contains one or more space characters (%x20), it
28332833+ is compared as a space-delimited list of values in which the order of
28342834+ values does not matter. Only one order of values can be registered,
28352835+ which covers all other arrangements of the same set of values.
28362836+28372837+ For example, the response type "token code" is left undefined by this
28382838+ specification. However, an extension can define and register the
28392839+ "token code" response type. Once registered, the same combination
28402840+ cannot be registered as "code token", but both values can be used to
28412841+ denote the same response type.
28422842+28432843+8.5. Defining Additional Error Codes
28442844+28452845+ In cases where protocol extensions (i.e., access token types,
28462846+ extension parameters, or extension grant types) require additional
28472847+ error codes to be used with the authorization code grant error
28482848+ response (Section 4.1.2.1), the implicit grant error response
28492849+ (Section 4.2.2.1), the token error response (Section 5.2), or the
28502850+ resource access error response (Section 7.2), such error codes MAY be
28512851+ defined.
28522852+28532853+28542854+28552855+28562856+28572857+28582858+Hardt Standards Track [Page 51]
28592859+28602860+RFC 6749 OAuth 2.0 October 2012
28612861+28622862+28632863+ Extension error codes MUST be registered (following the procedures in
28642864+ Section 11.4) if the extension they are used in conjunction with is a
28652865+ registered access token type, a registered endpoint parameter, or an
28662866+ extension grant type. Error codes used with unregistered extensions
28672867+ MAY be registered.
28682868+28692869+ Error codes MUST conform to the error ABNF and SHOULD be prefixed by
28702870+ an identifying name when possible. For example, an error identifying
28712871+ an invalid value set to the extension parameter "example" SHOULD be
28722872+ named "example_invalid".
28732873+28742874+ error = 1*error-char
28752875+ error-char = %x20-21 / %x23-5B / %x5D-7E
28762876+28772877+9. Native Applications
28782878+28792879+ Native applications are clients installed and executed on the device
28802880+ used by the resource owner (i.e., desktop application, native mobile
28812881+ application). Native applications require special consideration
28822882+ related to security, platform capabilities, and overall end-user
28832883+ experience.
28842884+28852885+ The authorization endpoint requires interaction between the client
28862886+ and the resource owner's user-agent. Native applications can invoke
28872887+ an external user-agent or embed a user-agent within the application.
28882888+ For example:
28892889+28902890+ o External user-agent - the native application can capture the
28912891+ response from the authorization server using a redirection URI
28922892+ with a scheme registered with the operating system to invoke the
28932893+ client as the handler, manual copy-and-paste of the credentials,
28942894+ running a local web server, installing a user-agent extension, or
28952895+ by providing a redirection URI identifying a server-hosted
28962896+ resource under the client's control, which in turn makes the
28972897+ response available to the native application.
28982898+28992899+ o Embedded user-agent - the native application obtains the response
29002900+ by directly communicating with the embedded user-agent by
29012901+ monitoring state changes emitted during the resource load, or
29022902+ accessing the user-agent's cookies storage.
29032903+29042904+ When choosing between an external or embedded user-agent, developers
29052905+ should consider the following:
29062906+29072907+ o An external user-agent may improve completion rate, as the
29082908+ resource owner may already have an active session with the
29092909+ authorization server, removing the need to re-authenticate. It
29102910+ provides a familiar end-user experience and functionality. The
29112911+29122912+29132913+29142914+Hardt Standards Track [Page 52]
29152915+29162916+RFC 6749 OAuth 2.0 October 2012
29172917+29182918+29192919+ resource owner may also rely on user-agent features or extensions
29202920+ to assist with authentication (e.g., password manager, 2-factor
29212921+ device reader).
29222922+29232923+ o An embedded user-agent may offer improved usability, as it removes
29242924+ the need to switch context and open new windows.
29252925+29262926+ o An embedded user-agent poses a security challenge because resource
29272927+ owners are authenticating in an unidentified window without access
29282928+ to the visual protections found in most external user-agents. An
29292929+ embedded user-agent educates end-users to trust unidentified
29302930+ requests for authentication (making phishing attacks easier to
29312931+ execute).
29322932+29332933+ When choosing between the implicit grant type and the authorization
29342934+ code grant type, the following should be considered:
29352935+29362936+ o Native applications that use the authorization code grant type
29372937+ SHOULD do so without using client credentials, due to the native
29382938+ application's inability to keep client credentials confidential.
29392939+29402940+ o When using the implicit grant type flow, a refresh token is not
29412941+ returned, which requires repeating the authorization process once
29422942+ the access token expires.
29432943+29442944+10. Security Considerations
29452945+29462946+ As a flexible and extensible framework, OAuth's security
29472947+ considerations depend on many factors. The following sections
29482948+ provide implementers with security guidelines focused on the three
29492949+ client profiles described in Section 2.1: web application,
29502950+ user-agent-based application, and native application.
29512951+29522952+ A comprehensive OAuth security model and analysis, as well as
29532953+ background for the protocol design, is provided by
29542954+ [OAuth-THREATMODEL].
29552955+29562956+10.1. Client Authentication
29572957+29582958+ The authorization server establishes client credentials with web
29592959+ application clients for the purpose of client authentication. The
29602960+ authorization server is encouraged to consider stronger client
29612961+ authentication means than a client password. Web application clients
29622962+ MUST ensure confidentiality of client passwords and other client
29632963+ credentials.
29642964+29652965+29662966+29672967+29682968+29692969+29702970+Hardt Standards Track [Page 53]
29712971+29722972+RFC 6749 OAuth 2.0 October 2012
29732973+29742974+29752975+ The authorization server MUST NOT issue client passwords or other
29762976+ client credentials to native application or user-agent-based
29772977+ application clients for the purpose of client authentication. The
29782978+ authorization server MAY issue a client password or other credentials
29792979+ for a specific installation of a native application client on a
29802980+ specific device.
29812981+29822982+ When client authentication is not possible, the authorization server
29832983+ SHOULD employ other means to validate the client's identity -- for
29842984+ example, by requiring the registration of the client redirection URI
29852985+ or enlisting the resource owner to confirm identity. A valid
29862986+ redirection URI is not sufficient to verify the client's identity
29872987+ when asking for resource owner authorization but can be used to
29882988+ prevent delivering credentials to a counterfeit client after
29892989+ obtaining resource owner authorization.
29902990+29912991+ The authorization server must consider the security implications of
29922992+ interacting with unauthenticated clients and take measures to limit
29932993+ the potential exposure of other credentials (e.g., refresh tokens)
29942994+ issued to such clients.
29952995+29962996+10.2. Client Impersonation
29972997+29982998+ A malicious client can impersonate another client and obtain access
29992999+ to protected resources if the impersonated client fails to, or is
30003000+ unable to, keep its client credentials confidential.
30013001+30023002+ The authorization server MUST authenticate the client whenever
30033003+ possible. If the authorization server cannot authenticate the client
30043004+ due to the client's nature, the authorization server MUST require the
30053005+ registration of any redirection URI used for receiving authorization
30063006+ responses and SHOULD utilize other means to protect resource owners
30073007+ from such potentially malicious clients. For example, the
30083008+ authorization server can engage the resource owner to assist in
30093009+ identifying the client and its origin.
30103010+30113011+ The authorization server SHOULD enforce explicit resource owner
30123012+ authentication and provide the resource owner with information about
30133013+ the client and the requested authorization scope and lifetime. It is
30143014+ up to the resource owner to review the information in the context of
30153015+ the current client and to authorize or deny the request.
30163016+30173017+ The authorization server SHOULD NOT process repeated authorization
30183018+ requests automatically (without active resource owner interaction)
30193019+ without authenticating the client or relying on other measures to
30203020+ ensure that the repeated request comes from the original client and
30213021+ not an impersonator.
30223022+30233023+30243024+30253025+30263026+Hardt Standards Track [Page 54]
30273027+30283028+RFC 6749 OAuth 2.0 October 2012
30293029+30303030+30313031+10.3. Access Tokens
30323032+30333033+ Access token credentials (as well as any confidential access token
30343034+ attributes) MUST be kept confidential in transit and storage, and
30353035+ only shared among the authorization server, the resource servers the
30363036+ access token is valid for, and the client to whom the access token is
30373037+ issued. Access token credentials MUST only be transmitted using TLS
30383038+ as described in Section 1.6 with server authentication as defined by
30393039+ [RFC2818].
30403040+30413041+ When using the implicit grant type, the access token is transmitted
30423042+ in the URI fragment, which can expose it to unauthorized parties.
30433043+30443044+ The authorization server MUST ensure that access tokens cannot be
30453045+ generated, modified, or guessed to produce valid access tokens by
30463046+ unauthorized parties.
30473047+30483048+ The client SHOULD request access tokens with the minimal scope
30493049+ necessary. The authorization server SHOULD take the client identity
30503050+ into account when choosing how to honor the requested scope and MAY
30513051+ issue an access token with less rights than requested.
30523052+30533053+ This specification does not provide any methods for the resource
30543054+ server to ensure that an access token presented to it by a given
30553055+ client was issued to that client by the authorization server.
30563056+30573057+10.4. Refresh Tokens
30583058+30593059+ Authorization servers MAY issue refresh tokens to web application
30603060+ clients and native application clients.
30613061+30623062+ Refresh tokens MUST be kept confidential in transit and storage, and
30633063+ shared only among the authorization server and the client to whom the
30643064+ refresh tokens were issued. The authorization server MUST maintain
30653065+ the binding between a refresh token and the client to whom it was
30663066+ issued. Refresh tokens MUST only be transmitted using TLS as
30673067+ described in Section 1.6 with server authentication as defined by
30683068+ [RFC2818].
30693069+30703070+ The authorization server MUST verify the binding between the refresh
30713071+ token and client identity whenever the client identity can be
30723072+ authenticated. When client authentication is not possible, the
30733073+ authorization server SHOULD deploy other means to detect refresh
30743074+ token abuse.
30753075+30763076+ For example, the authorization server could employ refresh token
30773077+ rotation in which a new refresh token is issued with every access
30783078+ token refresh response. The previous refresh token is invalidated
30793079+30803080+30813081+30823082+Hardt Standards Track [Page 55]
30833083+30843084+RFC 6749 OAuth 2.0 October 2012
30853085+30863086+30873087+ but retained by the authorization server. If a refresh token is
30883088+ compromised and subsequently used by both the attacker and the
30893089+ legitimate client, one of them will present an invalidated refresh
30903090+ token, which will inform the authorization server of the breach.
30913091+30923092+ The authorization server MUST ensure that refresh tokens cannot be
30933093+ generated, modified, or guessed to produce valid refresh tokens by
30943094+ unauthorized parties.
30953095+30963096+10.5. Authorization Codes
30973097+30983098+ The transmission of authorization codes SHOULD be made over a secure
30993099+ channel, and the client SHOULD require the use of TLS with its
31003100+ redirection URI if the URI identifies a network resource. Since
31013101+ authorization codes are transmitted via user-agent redirections, they
31023102+ could potentially be disclosed through user-agent history and HTTP
31033103+ referrer headers.
31043104+31053105+ Authorization codes operate as plaintext bearer credentials, used to
31063106+ verify that the resource owner who granted authorization at the
31073107+ authorization server is the same resource owner returning to the
31083108+ client to complete the process. Therefore, if the client relies on
31093109+ the authorization code for its own resource owner authentication, the
31103110+ client redirection endpoint MUST require the use of TLS.
31113111+31123112+ Authorization codes MUST be short lived and single-use. If the
31133113+ authorization server observes multiple attempts to exchange an
31143114+ authorization code for an access token, the authorization server
31153115+ SHOULD attempt to revoke all access tokens already granted based on
31163116+ the compromised authorization code.
31173117+31183118+ If the client can be authenticated, the authorization servers MUST
31193119+ authenticate the client and ensure that the authorization code was
31203120+ issued to the same client.
31213121+31223122+10.6. Authorization Code Redirection URI Manipulation
31233123+31243124+ When requesting authorization using the authorization code grant
31253125+ type, the client can specify a redirection URI via the "redirect_uri"
31263126+ parameter. If an attacker can manipulate the value of the
31273127+ redirection URI, it can cause the authorization server to redirect
31283128+ the resource owner user-agent to a URI under the control of the
31293129+ attacker with the authorization code.
31303130+31313131+ An attacker can create an account at a legitimate client and initiate
31323132+ the authorization flow. When the attacker's user-agent is sent to
31333133+ the authorization server to grant access, the attacker grabs the
31343134+ authorization URI provided by the legitimate client and replaces the
31353135+31363136+31373137+31383138+Hardt Standards Track [Page 56]
31393139+31403140+RFC 6749 OAuth 2.0 October 2012
31413141+31423142+31433143+ client's redirection URI with a URI under the control of the
31443144+ attacker. The attacker then tricks the victim into following the
31453145+ manipulated link to authorize access to the legitimate client.
31463146+31473147+ Once at the authorization server, the victim is prompted with a
31483148+ normal, valid request on behalf of a legitimate and trusted client,
31493149+ and authorizes the request. The victim is then redirected to an
31503150+ endpoint under the control of the attacker with the authorization
31513151+ code. The attacker completes the authorization flow by sending the
31523152+ authorization code to the client using the original redirection URI
31533153+ provided by the client. The client exchanges the authorization code
31543154+ with an access token and links it to the attacker's client account,
31553155+ which can now gain access to the protected resources authorized by
31563156+ the victim (via the client).
31573157+31583158+ In order to prevent such an attack, the authorization server MUST
31593159+ ensure that the redirection URI used to obtain the authorization code
31603160+ is identical to the redirection URI provided when exchanging the
31613161+ authorization code for an access token. The authorization server
31623162+ MUST require public clients and SHOULD require confidential clients
31633163+ to register their redirection URIs. If a redirection URI is provided
31643164+ in the request, the authorization server MUST validate it against the
31653165+ registered value.
31663166+31673167+10.7. Resource Owner Password Credentials
31683168+31693169+ The resource owner password credentials grant type is often used for
31703170+ legacy or migration reasons. It reduces the overall risk of storing
31713171+ usernames and passwords by the client but does not eliminate the need
31723172+ to expose highly privileged credentials to the client.
31733173+31743174+ This grant type carries a higher risk than other grant types because
31753175+ it maintains the password anti-pattern this protocol seeks to avoid.
31763176+ The client could abuse the password, or the password could
31773177+ unintentionally be disclosed to an attacker (e.g., via log files or
31783178+ other records kept by the client).
31793179+31803180+ Additionally, because the resource owner does not have control over
31813181+ the authorization process (the resource owner's involvement ends when
31823182+ it hands over its credentials to the client), the client can obtain
31833183+ access tokens with a broader scope than desired by the resource
31843184+ owner. The authorization server should consider the scope and
31853185+ lifetime of access tokens issued via this grant type.
31863186+31873187+ The authorization server and client SHOULD minimize use of this grant
31883188+ type and utilize other grant types whenever possible.
31893189+31903190+31913191+31923192+31933193+31943194+Hardt Standards Track [Page 57]
31953195+31963196+RFC 6749 OAuth 2.0 October 2012
31973197+31983198+31993199+10.8. Request Confidentiality
32003200+32013201+ Access tokens, refresh tokens, resource owner passwords, and client
32023202+ credentials MUST NOT be transmitted in the clear. Authorization
32033203+ codes SHOULD NOT be transmitted in the clear.
32043204+32053205+ The "state" and "scope" parameters SHOULD NOT include sensitive
32063206+ client or resource owner information in plain text, as they can be
32073207+ transmitted over insecure channels or stored insecurely.
32083208+32093209+10.9. Ensuring Endpoint Authenticity
32103210+32113211+ In order to prevent man-in-the-middle attacks, the authorization
32123212+ server MUST require the use of TLS with server authentication as
32133213+ defined by [RFC2818] for any request sent to the authorization and
32143214+ token endpoints. The client MUST validate the authorization server's
32153215+ TLS certificate as defined by [RFC6125] and in accordance with its
32163216+ requirements for server identity authentication.
32173217+32183218+10.10. Credentials-Guessing Attacks
32193219+32203220+ The authorization server MUST prevent attackers from guessing access
32213221+ tokens, authorization codes, refresh tokens, resource owner
32223222+ passwords, and client credentials.
32233223+32243224+ The probability of an attacker guessing generated tokens (and other
32253225+ credentials not intended for handling by end-users) MUST be less than
32263226+ or equal to 2^(-128) and SHOULD be less than or equal to 2^(-160).
32273227+32283228+ The authorization server MUST utilize other means to protect
32293229+ credentials intended for end-user usage.
32303230+32313231+10.11. Phishing Attacks
32323232+32333233+ Wide deployment of this and similar protocols may cause end-users to
32343234+ become inured to the practice of being redirected to websites where
32353235+ they are asked to enter their passwords. If end-users are not
32363236+ careful to verify the authenticity of these websites before entering
32373237+ their credentials, it will be possible for attackers to exploit this
32383238+ practice to steal resource owners' passwords.
32393239+32403240+ Service providers should attempt to educate end-users about the risks
32413241+ phishing attacks pose and should provide mechanisms that make it easy
32423242+ for end-users to confirm the authenticity of their sites. Client
32433243+ developers should consider the security implications of how they
32443244+ interact with the user-agent (e.g., external, embedded), and the
32453245+ ability of the end-user to verify the authenticity of the
32463246+ authorization server.
32473247+32483248+32493249+32503250+Hardt Standards Track [Page 58]
32513251+32523252+RFC 6749 OAuth 2.0 October 2012
32533253+32543254+32553255+ To reduce the risk of phishing attacks, the authorization servers
32563256+ MUST require the use of TLS on every endpoint used for end-user
32573257+ interaction.
32583258+32593259+10.12. Cross-Site Request Forgery
32603260+32613261+ Cross-site request forgery (CSRF) is an exploit in which an attacker
32623262+ causes the user-agent of a victim end-user to follow a malicious URI
32633263+ (e.g., provided to the user-agent as a misleading link, image, or
32643264+ redirection) to a trusting server (usually established via the
32653265+ presence of a valid session cookie).
32663266+32673267+ A CSRF attack against the client's redirection URI allows an attacker
32683268+ to inject its own authorization code or access token, which can
32693269+ result in the client using an access token associated with the
32703270+ attacker's protected resources rather than the victim's (e.g., save
32713271+ the victim's bank account information to a protected resource
32723272+ controlled by the attacker).
32733273+32743274+ The client MUST implement CSRF protection for its redirection URI.
32753275+ This is typically accomplished by requiring any request sent to the
32763276+ redirection URI endpoint to include a value that binds the request to
32773277+ the user-agent's authenticated state (e.g., a hash of the session
32783278+ cookie used to authenticate the user-agent). The client SHOULD
32793279+ utilize the "state" request parameter to deliver this value to the
32803280+ authorization server when making an authorization request.
32813281+32823282+ Once authorization has been obtained from the end-user, the
32833283+ authorization server redirects the end-user's user-agent back to the
32843284+ client with the required binding value contained in the "state"
32853285+ parameter. The binding value enables the client to verify the
32863286+ validity of the request by matching the binding value to the
32873287+ user-agent's authenticated state. The binding value used for CSRF
32883288+ protection MUST contain a non-guessable value (as described in
32893289+ Section 10.10), and the user-agent's authenticated state (e.g.,
32903290+ session cookie, HTML5 local storage) MUST be kept in a location
32913291+ accessible only to the client and the user-agent (i.e., protected by
32923292+ same-origin policy).
32933293+32943294+ A CSRF attack against the authorization server's authorization
32953295+ endpoint can result in an attacker obtaining end-user authorization
32963296+ for a malicious client without involving or alerting the end-user.
32973297+32983298+ The authorization server MUST implement CSRF protection for its
32993299+ authorization endpoint and ensure that a malicious client cannot
33003300+ obtain authorization without the awareness and explicit consent of
33013301+ the resource owner.
33023302+33033303+33043304+33053305+33063306+Hardt Standards Track [Page 59]
33073307+33083308+RFC 6749 OAuth 2.0 October 2012
33093309+33103310+33113311+10.13. Clickjacking
33123312+33133313+ In a clickjacking attack, an attacker registers a legitimate client
33143314+ and then constructs a malicious site in which it loads the
33153315+ authorization server's authorization endpoint web page in a
33163316+ transparent iframe overlaid on top of a set of dummy buttons, which
33173317+ are carefully constructed to be placed directly under important
33183318+ buttons on the authorization page. When an end-user clicks a
33193319+ misleading visible button, the end-user is actually clicking an
33203320+ invisible button on the authorization page (such as an "Authorize"
33213321+ button). This allows an attacker to trick a resource owner into
33223322+ granting its client access without the end-user's knowledge.
33233323+33243324+ To prevent this form of attack, native applications SHOULD use
33253325+ external browsers instead of embedding browsers within the
33263326+ application when requesting end-user authorization. For most newer
33273327+ browsers, avoidance of iframes can be enforced by the authorization
33283328+ server using the (non-standard) "x-frame-options" header. This
33293329+ header can have two values, "deny" and "sameorigin", which will block
33303330+ any framing, or framing by sites with a different origin,
33313331+ respectively. For older browsers, JavaScript frame-busting
33323332+ techniques can be used but may not be effective in all browsers.
33333333+33343334+10.14. Code Injection and Input Validation
33353335+33363336+ A code injection attack occurs when an input or otherwise external
33373337+ variable is used by an application unsanitized and causes
33383338+ modification to the application logic. This may allow an attacker to
33393339+ gain access to the application device or its data, cause denial of
33403340+ service, or introduce a wide range of malicious side-effects.
33413341+33423342+ The authorization server and client MUST sanitize (and validate when
33433343+ possible) any value received -- in particular, the value of the
33443344+ "state" and "redirect_uri" parameters.
33453345+33463346+10.15. Open Redirectors
33473347+33483348+ The authorization server, authorization endpoint, and client
33493349+ redirection endpoint can be improperly configured and operate as open
33503350+ redirectors. An open redirector is an endpoint using a parameter to
33513351+ automatically redirect a user-agent to the location specified by the
33523352+ parameter value without any validation.
33533353+33543354+ Open redirectors can be used in phishing attacks, or by an attacker
33553355+ to get end-users to visit malicious sites by using the URI authority
33563356+ component of a familiar and trusted destination. In addition, if the
33573357+ authorization server allows the client to register only part of the
33583358+ redirection URI, an attacker can use an open redirector operated by
33593359+33603360+33613361+33623362+Hardt Standards Track [Page 60]
33633363+33643364+RFC 6749 OAuth 2.0 October 2012
33653365+33663366+33673367+ the client to construct a redirection URI that will pass the
33683368+ authorization server validation but will send the authorization code
33693369+ or access token to an endpoint under the control of the attacker.
33703370+33713371+10.16. Misuse of Access Token to Impersonate Resource Owner in Implicit
33723372+ Flow
33733373+33743374+ For public clients using implicit flows, this specification does not
33753375+ provide any method for the client to determine what client an access
33763376+ token was issued to.
33773377+33783378+ A resource owner may willingly delegate access to a resource by
33793379+ granting an access token to an attacker's malicious client. This may
33803380+ be due to phishing or some other pretext. An attacker may also steal
33813381+ a token via some other mechanism. An attacker may then attempt to
33823382+ impersonate the resource owner by providing the access token to a
33833383+ legitimate public client.
33843384+33853385+ In the implicit flow (response_type=token), the attacker can easily
33863386+ switch the token in the response from the authorization server,
33873387+ replacing the real access token with the one previously issued to the
33883388+ attacker.
33893389+33903390+ Servers communicating with native applications that rely on being
33913391+ passed an access token in the back channel to identify the user of
33923392+ the client may be similarly compromised by an attacker creating a
33933393+ compromised application that can inject arbitrary stolen access
33943394+ tokens.
33953395+33963396+ Any public client that makes the assumption that only the resource
33973397+ owner can present it with a valid access token for the resource is
33983398+ vulnerable to this type of attack.
33993399+34003400+ This type of attack may expose information about the resource owner
34013401+ at the legitimate client to the attacker (malicious client). This
34023402+ will also allow the attacker to perform operations at the legitimate
34033403+ client with the same permissions as the resource owner who originally
34043404+ granted the access token or authorization code.
34053405+34063406+ Authenticating resource owners to clients is out of scope for this
34073407+ specification. Any specification that uses the authorization process
34083408+ as a form of delegated end-user authentication to the client (e.g.,
34093409+ third-party sign-in service) MUST NOT use the implicit flow without
34103410+ additional security mechanisms that would enable the client to
34113411+ determine if the access token was issued for its use (e.g., audience-
34123412+ restricting the access token).
34133413+34143414+34153415+34163416+34173417+34183418+Hardt Standards Track [Page 61]
34193419+34203420+RFC 6749 OAuth 2.0 October 2012
34213421+34223422+34233423+11. IANA Considerations
34243424+34253425+11.1. OAuth Access Token Types Registry
34263426+34273427+ This specification establishes the OAuth Access Token Types registry.
34283428+34293429+ Access token types are registered with a Specification Required
34303430+ ([RFC5226]) after a two-week review period on the
34313431+ oauth-ext-review@ietf.org mailing list, on the advice of one or more
34323432+ Designated Experts. However, to allow for the allocation of values
34333433+ prior to publication, the Designated Expert(s) may approve
34343434+ registration once they are satisfied that such a specification will
34353435+ be published.
34363436+34373437+ Registration requests must be sent to the oauth-ext-review@ietf.org
34383438+ mailing list for review and comment, with an appropriate subject
34393439+ (e.g., "Request for access token type: example").
34403440+34413441+ Within the review period, the Designated Expert(s) will either
34423442+ approve or deny the registration request, communicating this decision
34433443+ to the review list and IANA. Denials should include an explanation
34443444+ and, if applicable, suggestions as to how to make the request
34453445+ successful.
34463446+34473447+ IANA must only accept registry updates from the Designated Expert(s)
34483448+ and should direct all requests for registration to the review mailing
34493449+ list.
34503450+34513451+11.1.1. Registration Template
34523452+34533453+ Type name:
34543454+ The name requested (e.g., "example").
34553455+34563456+ Additional Token Endpoint Response Parameters:
34573457+ Additional response parameters returned together with the
34583458+ "access_token" parameter. New parameters MUST be separately
34593459+ registered in the OAuth Parameters registry as described by
34603460+ Section 11.2.
34613461+34623462+ HTTP Authentication Scheme(s):
34633463+ The HTTP authentication scheme name(s), if any, used to
34643464+ authenticate protected resource requests using access tokens of
34653465+ this type.
34663466+34673467+ Change controller:
34683468+ For Standards Track RFCs, state "IETF". For others, give the name
34693469+ of the responsible party. Other details (e.g., postal address,
34703470+ email address, home page URI) may also be included.
34713471+34723472+34733473+34743474+Hardt Standards Track [Page 62]
34753475+34763476+RFC 6749 OAuth 2.0 October 2012
34773477+34783478+34793479+ Specification document(s):
34803480+ Reference to the document(s) that specify the parameter,
34813481+ preferably including a URI that can be used to retrieve a copy of
34823482+ the document(s). An indication of the relevant sections may also
34833483+ be included but is not required.
34843484+34853485+11.2. OAuth Parameters Registry
34863486+34873487+ This specification establishes the OAuth Parameters registry.
34883488+34893489+ Additional parameters for inclusion in the authorization endpoint
34903490+ request, the authorization endpoint response, the token endpoint
34913491+ request, or the token endpoint response are registered with a
34923492+ Specification Required ([RFC5226]) after a two-week review period on
34933493+ the oauth-ext-review@ietf.org mailing list, on the advice of one or
34943494+ more Designated Experts. However, to allow for the allocation of
34953495+ values prior to publication, the Designated Expert(s) may approve
34963496+ registration once they are satisfied that such a specification will
34973497+ be published.
34983498+34993499+ Registration requests must be sent to the oauth-ext-review@ietf.org
35003500+ mailing list for review and comment, with an appropriate subject
35013501+ (e.g., "Request for parameter: example").
35023502+35033503+ Within the review period, the Designated Expert(s) will either
35043504+ approve or deny the registration request, communicating this decision
35053505+ to the review list and IANA. Denials should include an explanation
35063506+ and, if applicable, suggestions as to how to make the request
35073507+ successful.
35083508+35093509+ IANA must only accept registry updates from the Designated Expert(s)
35103510+ and should direct all requests for registration to the review mailing
35113511+ list.
35123512+35133513+11.2.1. Registration Template
35143514+35153515+ Parameter name:
35163516+ The name requested (e.g., "example").
35173517+35183518+ Parameter usage location:
35193519+ The location(s) where parameter can be used. The possible
35203520+ locations are authorization request, authorization response, token
35213521+ request, or token response.
35223522+35233523+ Change controller:
35243524+ For Standards Track RFCs, state "IETF". For others, give the name
35253525+ of the responsible party. Other details (e.g., postal address,
35263526+ email address, home page URI) may also be included.
35273527+35283528+35293529+35303530+Hardt Standards Track [Page 63]
35313531+35323532+RFC 6749 OAuth 2.0 October 2012
35333533+35343534+35353535+ Specification document(s):
35363536+ Reference to the document(s) that specify the parameter,
35373537+ preferably including a URI that can be used to retrieve a copy of
35383538+ the document(s). An indication of the relevant sections may also
35393539+ be included but is not required.
35403540+35413541+11.2.2. Initial Registry Contents
35423542+35433543+ The OAuth Parameters registry's initial contents are:
35443544+35453545+ o Parameter name: client_id
35463546+ o Parameter usage location: authorization request, token request
35473547+ o Change controller: IETF
35483548+ o Specification document(s): RFC 6749
35493549+35503550+ o Parameter name: client_secret
35513551+ o Parameter usage location: token request
35523552+ o Change controller: IETF
35533553+ o Specification document(s): RFC 6749
35543554+35553555+ o Parameter name: response_type
35563556+ o Parameter usage location: authorization request
35573557+ o Change controller: IETF
35583558+ o Specification document(s): RFC 6749
35593559+35603560+ o Parameter name: redirect_uri
35613561+ o Parameter usage location: authorization request, token request
35623562+ o Change controller: IETF
35633563+ o Specification document(s): RFC 6749
35643564+35653565+ o Parameter name: scope
35663566+ o Parameter usage location: authorization request, authorization
35673567+ response, token request, token response
35683568+ o Change controller: IETF
35693569+ o Specification document(s): RFC 6749
35703570+35713571+ o Parameter name: state
35723572+ o Parameter usage location: authorization request, authorization
35733573+ response
35743574+ o Change controller: IETF
35753575+ o Specification document(s): RFC 6749
35763576+35773577+ o Parameter name: code
35783578+ o Parameter usage location: authorization response, token request
35793579+ o Change controller: IETF
35803580+ o Specification document(s): RFC 6749
35813581+35823582+35833583+35843584+35853585+35863586+Hardt Standards Track [Page 64]
35873587+35883588+RFC 6749 OAuth 2.0 October 2012
35893589+35903590+35913591+ o Parameter name: error_description
35923592+ o Parameter usage location: authorization response, token response
35933593+ o Change controller: IETF
35943594+ o Specification document(s): RFC 6749
35953595+35963596+ o Parameter name: error_uri
35973597+ o Parameter usage location: authorization response, token response
35983598+ o Change controller: IETF
35993599+ o Specification document(s): RFC 6749
36003600+36013601+ o Parameter name: grant_type
36023602+ o Parameter usage location: token request
36033603+ o Change controller: IETF
36043604+ o Specification document(s): RFC 6749
36053605+36063606+ o Parameter name: access_token
36073607+ o Parameter usage location: authorization response, token response
36083608+ o Change controller: IETF
36093609+ o Specification document(s): RFC 6749
36103610+36113611+ o Parameter name: token_type
36123612+ o Parameter usage location: authorization response, token response
36133613+ o Change controller: IETF
36143614+ o Specification document(s): RFC 6749
36153615+36163616+ o Parameter name: expires_in
36173617+ o Parameter usage location: authorization response, token response
36183618+ o Change controller: IETF
36193619+ o Specification document(s): RFC 6749
36203620+36213621+ o Parameter name: username
36223622+ o Parameter usage location: token request
36233623+ o Change controller: IETF
36243624+ o Specification document(s): RFC 6749
36253625+36263626+ o Parameter name: password
36273627+ o Parameter usage location: token request
36283628+ o Change controller: IETF
36293629+ o Specification document(s): RFC 6749
36303630+36313631+ o Parameter name: refresh_token
36323632+ o Parameter usage location: token request, token response
36333633+ o Change controller: IETF
36343634+ o Specification document(s): RFC 6749
36353635+36363636+36373637+36383638+36393639+36403640+36413641+36423642+Hardt Standards Track [Page 65]
36433643+36443644+RFC 6749 OAuth 2.0 October 2012
36453645+36463646+36473647+11.3. OAuth Authorization Endpoint Response Types Registry
36483648+36493649+ This specification establishes the OAuth Authorization Endpoint
36503650+ Response Types registry.
36513651+36523652+ Additional response types for use with the authorization endpoint are
36533653+ registered with a Specification Required ([RFC5226]) after a two-week
36543654+ review period on the oauth-ext-review@ietf.org mailing list, on the
36553655+ advice of one or more Designated Experts. However, to allow for the
36563656+ allocation of values prior to publication, the Designated Expert(s)
36573657+ may approve registration once they are satisfied that such a
36583658+ specification will be published.
36593659+36603660+ Registration requests must be sent to the oauth-ext-review@ietf.org
36613661+ mailing list for review and comment, with an appropriate subject
36623662+ (e.g., "Request for response type: example").
36633663+36643664+ Within the review period, the Designated Expert(s) will either
36653665+ approve or deny the registration request, communicating this decision
36663666+ to the review list and IANA. Denials should include an explanation
36673667+ and, if applicable, suggestions as to how to make the request
36683668+ successful.
36693669+36703670+ IANA must only accept registry updates from the Designated Expert(s)
36713671+ and should direct all requests for registration to the review mailing
36723672+ list.
36733673+36743674+11.3.1. Registration Template
36753675+36763676+ Response type name:
36773677+ The name requested (e.g., "example").
36783678+36793679+ Change controller:
36803680+ For Standards Track RFCs, state "IETF". For others, give the name
36813681+ of the responsible party. Other details (e.g., postal address,
36823682+ email address, home page URI) may also be included.
36833683+36843684+ Specification document(s):
36853685+ Reference to the document(s) that specify the type, preferably
36863686+ including a URI that can be used to retrieve a copy of the
36873687+ document(s). An indication of the relevant sections may also be
36883688+ included but is not required.
36893689+36903690+36913691+36923692+36933693+36943694+36953695+36963696+36973697+36983698+Hardt Standards Track [Page 66]
36993699+37003700+RFC 6749 OAuth 2.0 October 2012
37013701+37023702+37033703+11.3.2. Initial Registry Contents
37043704+37053705+ The OAuth Authorization Endpoint Response Types registry's initial
37063706+ contents are:
37073707+37083708+ o Response type name: code
37093709+ o Change controller: IETF
37103710+ o Specification document(s): RFC 6749
37113711+37123712+ o Response type name: token
37133713+ o Change controller: IETF
37143714+ o Specification document(s): RFC 6749
37153715+37163716+11.4. OAuth Extensions Error Registry
37173717+37183718+ This specification establishes the OAuth Extensions Error registry.
37193719+37203720+ Additional error codes used together with other protocol extensions
37213721+ (i.e., extension grant types, access token types, or extension
37223722+ parameters) are registered with a Specification Required ([RFC5226])
37233723+ after a two-week review period on the oauth-ext-review@ietf.org
37243724+ mailing list, on the advice of one or more Designated Experts.
37253725+ However, to allow for the allocation of values prior to publication,
37263726+ the Designated Expert(s) may approve registration once they are
37273727+ satisfied that such a specification will be published.
37283728+37293729+ Registration requests must be sent to the oauth-ext-review@ietf.org
37303730+ mailing list for review and comment, with an appropriate subject
37313731+ (e.g., "Request for error code: example").
37323732+37333733+ Within the review period, the Designated Expert(s) will either
37343734+ approve or deny the registration request, communicating this decision
37353735+ to the review list and IANA. Denials should include an explanation
37363736+ and, if applicable, suggestions as to how to make the request
37373737+ successful.
37383738+37393739+ IANA must only accept registry updates from the Designated Expert(s)
37403740+ and should direct all requests for registration to the review mailing
37413741+ list.
37423742+37433743+37443744+37453745+37463746+37473747+37483748+37493749+37503750+37513751+37523752+37533753+37543754+Hardt Standards Track [Page 67]
37553755+37563756+RFC 6749 OAuth 2.0 October 2012
37573757+37583758+37593759+11.4.1. Registration Template
37603760+37613761+ Error name:
37623762+ The name requested (e.g., "example"). Values for the error name
37633763+ MUST NOT include characters outside the set %x20-21 / %x23-5B /
37643764+ %x5D-7E.
37653765+37663766+ Error usage location:
37673767+ The location(s) where the error can be used. The possible
37683768+ locations are authorization code grant error response
37693769+ (Section 4.1.2.1), implicit grant error response
37703770+ (Section 4.2.2.1), token error response (Section 5.2), or resource
37713771+ access error response (Section 7.2).
37723772+37733773+ Related protocol extension:
37743774+ The name of the extension grant type, access token type, or
37753775+ extension parameter that the error code is used in conjunction
37763776+ with.
37773777+37783778+ Change controller:
37793779+ For Standards Track RFCs, state "IETF". For others, give the name
37803780+ of the responsible party. Other details (e.g., postal address,
37813781+ email address, home page URI) may also be included.
37823782+37833783+ Specification document(s):
37843784+ Reference to the document(s) that specify the error code,
37853785+ preferably including a URI that can be used to retrieve a copy of
37863786+ the document(s). An indication of the relevant sections may also
37873787+ be included but is not required.
37883788+37893789+12. References
37903790+37913791+12.1. Normative References
37923792+37933793+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
37943794+ Requirement Levels", BCP 14, RFC 2119, March 1997.
37953795+37963796+ [RFC2246] Dierks, T. and C. Allen, "The TLS Protocol Version 1.0",
37973797+ RFC 2246, January 1999.
37983798+37993799+ [RFC2616] Fielding, R., Gettys, J., Mogul, J., Frystyk, H.,
38003800+ Masinter, L., Leach, P., and T. Berners-Lee, "Hypertext
38013801+ Transfer Protocol -- HTTP/1.1", RFC 2616, June 1999.
38023802+38033803+ [RFC2617] Franks, J., Hallam-Baker, P., Hostetler, J., Lawrence, S.,
38043804+ Leach, P., Luotonen, A., and L. Stewart, "HTTP
38053805+ Authentication: Basic and Digest Access Authentication",
38063806+ RFC 2617, June 1999.
38073807+38083808+38093809+38103810+Hardt Standards Track [Page 68]
38113811+38123812+RFC 6749 OAuth 2.0 October 2012
38133813+38143814+38153815+ [RFC2818] Rescorla, E., "HTTP Over TLS", RFC 2818, May 2000.
38163816+38173817+ [RFC3629] Yergeau, F., "UTF-8, a transformation format of
38183818+ ISO 10646", STD 63, RFC 3629, November 2003.
38193819+38203820+ [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform
38213821+ Resource Identifier (URI): Generic Syntax", STD 66,
38223822+ RFC 3986, January 2005.
38233823+38243824+ [RFC4627] Crockford, D., "The application/json Media Type for
38253825+ JavaScript Object Notation (JSON)", RFC 4627, July 2006.
38263826+38273827+ [RFC4949] Shirey, R., "Internet Security Glossary, Version 2",
38283828+ RFC 4949, August 2007.
38293829+38303830+ [RFC5226] Narten, T. and H. Alvestrand, "Guidelines for Writing an
38313831+ IANA Considerations Section in RFCs", BCP 26, RFC 5226,
38323832+ May 2008.
38333833+38343834+ [RFC5234] Crocker, D. and P. Overell, "Augmented BNF for Syntax
38353835+ Specifications: ABNF", STD 68, RFC 5234, January 2008.
38363836+38373837+ [RFC5246] Dierks, T. and E. Rescorla, "The Transport Layer Security
38383838+ (TLS) Protocol Version 1.2", RFC 5246, August 2008.
38393839+38403840+ [RFC6125] Saint-Andre, P. and J. Hodges, "Representation and
38413841+ Verification of Domain-Based Application Service Identity
38423842+ within Internet Public Key Infrastructure Using X.509
38433843+ (PKIX) Certificates in the Context of Transport Layer
38443844+ Security (TLS)", RFC 6125, March 2011.
38453845+38463846+ [USASCII] American National Standards Institute, "Coded Character
38473847+ Set -- 7-bit American Standard Code for Information
38483848+ Interchange", ANSI X3.4, 1986.
38493849+38503850+ [W3C.REC-html401-19991224]
38513851+ Raggett, D., Le Hors, A., and I. Jacobs, "HTML 4.01
38523852+ Specification", World Wide Web Consortium
38533853+ Recommendation REC-html401-19991224, December 1999,
38543854+ <http://www.w3.org/TR/1999/REC-html401-19991224>.
38553855+38563856+ [W3C.REC-xml-20081126]
38573857+ Bray, T., Paoli, J., Sperberg-McQueen, C., Maler, E.,
38583858+ and F. Yergeau, "Extensible Markup Language (XML) 1.0
38593859+ (Fifth Edition)", World Wide Web Consortium
38603860+ Recommendation REC-xml-20081126, November 2008,
38613861+ <http://www.w3.org/TR/2008/REC-xml-20081126>.
38623862+38633863+38643864+38653865+38663866+Hardt Standards Track [Page 69]
38673867+38683868+RFC 6749 OAuth 2.0 October 2012
38693869+38703870+38713871+12.2. Informative References
38723872+38733873+ [OAuth-HTTP-MAC]
38743874+ Hammer-Lahav, E., Ed., "HTTP Authentication: MAC Access
38753875+ Authentication", Work in Progress, February 2012.
38763876+38773877+ [OAuth-SAML2]
38783878+ Campbell, B. and C. Mortimore, "SAML 2.0 Bearer Assertion
38793879+ Profiles for OAuth 2.0", Work in Progress, September 2012.
38803880+38813881+ [OAuth-THREATMODEL]
38823882+ Lodderstedt, T., Ed., McGloin, M., and P. Hunt, "OAuth 2.0
38833883+ Threat Model and Security Considerations", Work
38843884+ in Progress, October 2012.
38853885+38863886+ [OAuth-WRAP]
38873887+ Hardt, D., Ed., Tom, A., Eaton, B., and Y. Goland, "OAuth
38883888+ Web Resource Authorization Profiles", Work in Progress,
38893889+ January 2010.
38903890+38913891+ [RFC5849] Hammer-Lahav, E., "The OAuth 1.0 Protocol", RFC 5849,
38923892+ April 2010.
38933893+38943894+ [RFC6750] Jones, M. and D. Hardt, "The OAuth 2.0 Authorization
38953895+ Framework: Bearer Token Usage", RFC 6750, October 2012.
38963896+38973897+38983898+38993899+39003900+39013901+39023902+39033903+39043904+39053905+39063906+39073907+39083908+39093909+39103910+39113911+39123912+39133913+39143914+39153915+39163916+39173917+39183918+39193919+39203920+39213921+39223922+Hardt Standards Track [Page 70]
39233923+39243924+RFC 6749 OAuth 2.0 October 2012
39253925+39263926+39273927+Appendix A. Augmented Backus-Naur Form (ABNF) Syntax
39283928+39293929+ This section provides Augmented Backus-Naur Form (ABNF) syntax
39303930+ descriptions for the elements defined in this specification using the
39313931+ notation of [RFC5234]. The ABNF below is defined in terms of Unicode
39323932+ code points [W3C.REC-xml-20081126]; these characters are typically
39333933+ encoded in UTF-8. Elements are presented in the order first defined.
39343934+39353935+ Some of the definitions that follow use the "URI-reference"
39363936+ definition from [RFC3986].
39373937+39383938+ Some of the definitions that follow use these common definitions:
39393939+39403940+ VSCHAR = %x20-7E
39413941+ NQCHAR = %x21 / %x23-5B / %x5D-7E
39423942+ NQSCHAR = %x20-21 / %x23-5B / %x5D-7E
39433943+ UNICODECHARNOCRLF = %x09 /%x20-7E / %x80-D7FF /
39443944+ %xE000-FFFD / %x10000-10FFFF
39453945+39463946+ (The UNICODECHARNOCRLF definition is based upon the Char definition
39473947+ in Section 2.2 of [W3C.REC-xml-20081126], but omitting the Carriage
39483948+ Return and Linefeed characters.)
39493949+39503950+A.1. "client_id" Syntax
39513951+39523952+ The "client_id" element is defined in Section 2.3.1:
39533953+39543954+ client-id = *VSCHAR
39553955+39563956+A.2. "client_secret" Syntax
39573957+39583958+ The "client_secret" element is defined in Section 2.3.1:
39593959+39603960+ client-secret = *VSCHAR
39613961+39623962+A.3. "response_type" Syntax
39633963+39643964+ The "response_type" element is defined in Sections 3.1.1 and 8.4:
39653965+39663966+ response-type = response-name *( SP response-name )
39673967+ response-name = 1*response-char
39683968+ response-char = "_" / DIGIT / ALPHA
39693969+39703970+39713971+39723972+39733973+39743974+39753975+39763976+39773977+39783978+Hardt Standards Track [Page 71]
39793979+39803980+RFC 6749 OAuth 2.0 October 2012
39813981+39823982+39833983+A.4. "scope" Syntax
39843984+39853985+ The "scope" element is defined in Section 3.3:
39863986+39873987+ scope = scope-token *( SP scope-token )
39883988+ scope-token = 1*NQCHAR
39893989+39903990+A.5. "state" Syntax
39913991+39923992+ The "state" element is defined in Sections 4.1.1, 4.1.2, 4.1.2.1,
39933993+ 4.2.1, 4.2.2, and 4.2.2.1:
39943994+39953995+ state = 1*VSCHAR
39963996+39973997+A.6. "redirect_uri" Syntax
39983998+39993999+ The "redirect_uri" element is defined in Sections 4.1.1, 4.1.3,
40004000+ and 4.2.1:
40014001+40024002+ redirect-uri = URI-reference
40034003+40044004+A.7. "error" Syntax
40054005+40064006+ The "error" element is defined in Sections 4.1.2.1, 4.2.2.1, 5.2,
40074007+ 7.2, and 8.5:
40084008+40094009+ error = 1*NQSCHAR
40104010+40114011+A.8. "error_description" Syntax
40124012+40134013+ The "error_description" element is defined in Sections 4.1.2.1,
40144014+ 4.2.2.1, 5.2, and 7.2:
40154015+40164016+ error-description = 1*NQSCHAR
40174017+40184018+A.9. "error_uri" Syntax
40194019+40204020+ The "error_uri" element is defined in Sections 4.1.2.1, 4.2.2.1, 5.2,
40214021+ and 7.2:
40224022+40234023+ error-uri = URI-reference
40244024+40254025+40264026+40274027+40284028+40294029+40304030+40314031+40324032+40334033+40344034+Hardt Standards Track [Page 72]
40354035+40364036+RFC 6749 OAuth 2.0 October 2012
40374037+40384038+40394039+A.10. "grant_type" Syntax
40404040+40414041+ The "grant_type" element is defined in Sections 4.1.3, 4.3.2, 4.4.2,
40424042+ 4.5, and 6:
40434043+40444044+ grant-type = grant-name / URI-reference
40454045+ grant-name = 1*name-char
40464046+ name-char = "-" / "." / "_" / DIGIT / ALPHA
40474047+40484048+A.11. "code" Syntax
40494049+40504050+ The "code" element is defined in Section 4.1.3:
40514051+40524052+ code = 1*VSCHAR
40534053+40544054+A.12. "access_token" Syntax
40554055+40564056+ The "access_token" element is defined in Sections 4.2.2 and 5.1:
40574057+40584058+ access-token = 1*VSCHAR
40594059+40604060+A.13. "token_type" Syntax
40614061+40624062+ The "token_type" element is defined in Sections 4.2.2, 5.1, and 8.1:
40634063+40644064+ token-type = type-name / URI-reference
40654065+ type-name = 1*name-char
40664066+ name-char = "-" / "." / "_" / DIGIT / ALPHA
40674067+40684068+A.14. "expires_in" Syntax
40694069+40704070+ The "expires_in" element is defined in Sections 4.2.2 and 5.1:
40714071+40724072+ expires-in = 1*DIGIT
40734073+40744074+A.15. "username" Syntax
40754075+40764076+ The "username" element is defined in Section 4.3.2:
40774077+40784078+ username = *UNICODECHARNOCRLF
40794079+40804080+A.16. "password" Syntax
40814081+40824082+ The "password" element is defined in Section 4.3.2:
40834083+40844084+ password = *UNICODECHARNOCRLF
40854085+40864086+40874087+40884088+40894089+40904090+Hardt Standards Track [Page 73]
40914091+40924092+RFC 6749 OAuth 2.0 October 2012
40934093+40944094+40954095+A.17. "refresh_token" Syntax
40964096+40974097+ The "refresh_token" element is defined in Sections 5.1 and 6:
40984098+40994099+ refresh-token = 1*VSCHAR
41004100+41014101+A.18. Endpoint Parameter Syntax
41024102+41034103+ The syntax for new endpoint parameters is defined in Section 8.2:
41044104+41054105+ param-name = 1*name-char
41064106+ name-char = "-" / "." / "_" / DIGIT / ALPHA
41074107+41084108+Appendix B. Use of application/x-www-form-urlencoded Media Type
41094109+41104110+ At the time of publication of this specification, the
41114111+ "application/x-www-form-urlencoded" media type was defined in
41124112+ Section 17.13.4 of [W3C.REC-html401-19991224] but not registered in
41134113+ the IANA MIME Media Types registry
41144114+ (<http://www.iana.org/assignments/media-types>). Furthermore, that
41154115+ definition is incomplete, as it does not consider non-US-ASCII
41164116+ characters.
41174117+41184118+ To address this shortcoming when generating payloads using this media
41194119+ type, names and values MUST be encoded using the UTF-8 character
41204120+ encoding scheme [RFC3629] first; the resulting octet sequence then
41214121+ needs to be further encoded using the escaping rules defined in
41224122+ [W3C.REC-html401-19991224].
41234123+41244124+ When parsing data from a payload using this media type, the names and
41254125+ values resulting from reversing the name/value encoding consequently
41264126+ need to be treated as octet sequences, to be decoded using the UTF-8
41274127+ character encoding scheme.
41284128+41294129+ For example, the value consisting of the six Unicode code points
41304130+ (1) U+0020 (SPACE), (2) U+0025 (PERCENT SIGN),
41314131+ (3) U+0026 (AMPERSAND), (4) U+002B (PLUS SIGN),
41324132+ (5) U+00A3 (POUND SIGN), and (6) U+20AC (EURO SIGN) would be encoded
41334133+ into the octet sequence below (using hexadecimal notation):
41344134+41354135+ 20 25 26 2B C2 A3 E2 82 AC
41364136+41374137+ and then represented in the payload as:
41384138+41394139+ +%25%26%2B%C2%A3%E2%82%AC
41404140+41414141+41424142+41434143+41444144+41454145+41464146+Hardt Standards Track [Page 74]
41474147+41484148+RFC 6749 OAuth 2.0 October 2012
41494149+41504150+41514151+Appendix C. Acknowledgements
41524152+41534153+ The initial OAuth 2.0 protocol specification was edited by David
41544154+ Recordon, based on two previous publications: the OAuth 1.0 community
41554155+ specification [RFC5849], and OAuth WRAP (OAuth Web Resource
41564156+ Authorization Profiles) [OAuth-WRAP]. Eran Hammer then edited many
41574157+ of the intermediate drafts that evolved into this RFC. The Security
41584158+ Considerations section was drafted by Torsten Lodderstedt, Mark
41594159+ McGloin, Phil Hunt, Anthony Nadalin, and John Bradley. The section
41604160+ on use of the "application/x-www-form-urlencoded" media type was
41614161+ drafted by Julian Reschke. The ABNF section was drafted by Michael
41624162+ B. Jones.
41634163+41644164+ The OAuth 1.0 community specification was edited by Eran Hammer and
41654165+ authored by Mark Atwood, Dirk Balfanz, Darren Bounds, Richard M.
41664166+ Conlan, Blaine Cook, Leah Culver, Breno de Medeiros, Brian Eaton,
41674167+ Kellan Elliott-McCrea, Larry Halff, Eran Hammer, Ben Laurie, Chris
41684168+ Messina, John Panzer, Sam Quigley, David Recordon, Eran Sandler,
41694169+ Jonathan Sergent, Todd Sieling, Brian Slesinsky, and Andy Smith.
41704170+41714171+ The OAuth WRAP specification was edited by Dick Hardt and authored by
41724172+ Brian Eaton, Yaron Y. Goland, Dick Hardt, and Allen Tom.
41734173+41744174+ This specification is the work of the OAuth Working Group, which
41754175+ includes dozens of active and dedicated participants. In particular,
41764176+ the following individuals contributed ideas, feedback, and wording
41774177+ that shaped and formed the final specification:
41784178+41794179+ Michael Adams, Amanda Anganes, Andrew Arnott, Dirk Balfanz, Aiden
41804180+ Bell, John Bradley, Marcos Caceres, Brian Campbell, Scott Cantor,
41814181+ Blaine Cook, Roger Crew, Leah Culver, Bill de hOra, Andre DeMarre,
41824182+ Brian Eaton, Wesley Eddy, Wolter Eldering, Brian Ellin, Igor
41834183+ Faynberg, George Fletcher, Tim Freeman, Luca Frosini, Evan Gilbert,
41844184+ Yaron Y. Goland, Brent Goldman, Kristoffer Gronowski, Eran Hammer,
41854185+ Dick Hardt, Justin Hart, Craig Heath, Phil Hunt, Michael B. Jones,
41864186+ Terry Jones, John Kemp, Mark Kent, Raffi Krikorian, Chasen Le Hara,
41874187+ Rasmus Lerdorf, Torsten Lodderstedt, Hui-Lan Lu, Casey Lucas, Paul
41884188+ Madsen, Alastair Mair, Eve Maler, James Manger, Mark McGloin,
41894189+ Laurence Miao, William Mills, Chuck Mortimore, Anthony Nadalin,
41904190+ Julian Reschke, Justin Richer, Peter Saint-Andre, Nat Sakimura, Rob
41914191+ Sayre, Marius Scurtescu, Naitik Shah, Luke Shepard, Vlad Skvortsov,
41924192+ Justin Smith, Haibin Song, Niv Steingarten, Christian Stuebner,
41934193+ Jeremy Suriel, Paul Tarjan, Christopher Thomas, Henry S. Thompson,
41944194+ Allen Tom, Franklin Tse, Nick Walker, Shane Weeden, and Skylar
41954195+ Woodward.
41964196+41974197+41984198+41994199+42004200+42014201+42024202+Hardt Standards Track [Page 75]
42034203+42044204+RFC 6749 OAuth 2.0 October 2012
42054205+42064206+42074207+ This document was produced under the chairmanship of Blaine Cook,
42084208+ Peter Saint-Andre, Hannes Tschofenig, Barry Leiba, and Derek Atkins.
42094209+ The area directors included Lisa Dusseault, Peter Saint-Andre, and
42104210+ Stephen Farrell.
42114211+42124212+Author's Address
42134213+42144214+ Dick Hardt (editor)
42154215+ Microsoft
42164216+42174217+ EMail: dick.hardt@gmail.com
42184218+ URI: http://dickhardt.org/
42194219+42204220+42214221+42224222+42234223+42244224+42254225+42264226+42274227+42284228+42294229+42304230+42314231+42324232+42334233+42344234+42354235+42364236+42374237+42384238+42394239+42404240+42414241+42424242+42434243+42444244+42454245+42464246+42474247+42484248+42494249+42504250+42514251+42524252+42534253+42544254+42554255+42564256+42574257+42584258+Hardt Standards Track [Page 76]
42594259+
+1123
spec/rfc7636.txt
···11+22+33+44+55+66+77+Internet Engineering Task Force (IETF) N. Sakimura, Ed.
88+Request for Comments: 7636 Nomura Research Institute
99+Category: Standards Track J. Bradley
1010+ISSN: 2070-1721 Ping Identity
1111+ N. Agarwal
1212+ Google
1313+ September 2015
1414+1515+1616+ Proof Key for Code Exchange by OAuth Public Clients
1717+1818+Abstract
1919+2020+ OAuth 2.0 public clients utilizing the Authorization Code Grant are
2121+ susceptible to the authorization code interception attack. This
2222+ specification describes the attack as well as a technique to mitigate
2323+ against the threat through the use of Proof Key for Code Exchange
2424+ (PKCE, pronounced "pixy").
2525+2626+Status of This Memo
2727+2828+ This is an Internet Standards Track document.
2929+3030+ This document is a product of the Internet Engineering Task Force
3131+ (IETF). It represents the consensus of the IETF community. It has
3232+ received public review and has been approved for publication by the
3333+ Internet Engineering Steering Group (IESG). Further information on
3434+ Internet Standards is available in Section 2 of RFC 5741.
3535+3636+ Information about the current status of this document, any errata,
3737+ and how to provide feedback on it may be obtained at
3838+ http://www.rfc-editor.org/info/rfc7636.
3939+4040+Copyright Notice
4141+4242+ Copyright (c) 2015 IETF Trust and the persons identified as the
4343+ document authors. All rights reserved.
4444+4545+ This document is subject to BCP 78 and the IETF Trust's Legal
4646+ Provisions Relating to IETF Documents
4747+ (http://trustee.ietf.org/license-info) in effect on the date of
4848+ publication of this document. Please review these documents
4949+ carefully, as they describe your rights and restrictions with respect
5050+ to this document. Code Components extracted from this document must
5151+ include Simplified BSD License text as described in Section 4.e of
5252+ the Trust Legal Provisions and are provided without warranty as
5353+ described in the Simplified BSD License.
5454+5555+5656+5757+5858+Sakimura, et al. Standards Track [Page 1]
5959+6060+RFC 7636 OAUTH PKCE September 2015
6161+6262+6363+Table of Contents
6464+6565+ 1. Introduction ....................................................3
6666+ 1.1. Protocol Flow ..............................................5
6767+ 2. Notational Conventions ..........................................6
6868+ 3. Terminology .....................................................7
6969+ 3.1. Abbreviations ..............................................7
7070+ 4. Protocol ........................................................8
7171+ 4.1. Client Creates a Code Verifier .............................8
7272+ 4.2. Client Creates the Code Challenge ..........................8
7373+ 4.3. Client Sends the Code Challenge with the
7474+ Authorization Request ......................................9
7575+ 4.4. Server Returns the Code ....................................9
7676+ 4.4.1. Error Response ......................................9
7777+ 4.5. Client Sends the Authorization Code and the Code
7878+ Verifier to the Token Endpoint ............................10
7979+ 4.6. Server Verifies code_verifier before Returning the
8080+ Tokens ....................................................10
8181+ 5. Compatibility ..................................................11
8282+ 6. IANA Considerations ............................................11
8383+ 6.1. OAuth Parameters Registry .................................11
8484+ 6.2. PKCE Code Challenge Method Registry .......................11
8585+ 6.2.1. Registration Template ..............................12
8686+ 6.2.2. Initial Registry Contents ..........................13
8787+ 7. Security Considerations ........................................13
8888+ 7.1. Entropy of the code_verifier ..............................13
8989+ 7.2. Protection against Eavesdroppers ..........................13
9090+ 7.3. Salting the code_challenge ................................14
9191+ 7.4. OAuth Security Considerations .............................14
9292+ 7.5. TLS Security Considerations ...............................15
9393+ 8. References .....................................................15
9494+ 8.1. Normative References ......................................15
9595+ 8.2. Informative References ....................................16
9696+ Appendix A. Notes on Implementing Base64url Encoding without
9797+ Padding .............................................17
9898+ Appendix B. Example for the S256 code_challenge_method ...........17
9999+ Acknowledgements ..................................................19
100100+ Authors' Addresses ................................................20
101101+102102+103103+104104+105105+106106+107107+108108+109109+110110+111111+112112+113113+114114+Sakimura, et al. Standards Track [Page 2]
115115+116116+RFC 7636 OAUTH PKCE September 2015
117117+118118+119119+1. Introduction
120120+121121+ OAuth 2.0 [RFC6749] public clients are susceptible to the
122122+ authorization code interception attack.
123123+124124+ In this attack, the attacker intercepts the authorization code
125125+ returned from the authorization endpoint within a communication path
126126+ not protected by Transport Layer Security (TLS), such as inter-
127127+ application communication within the client's operating system.
128128+129129+ Once the attacker has gained access to the authorization code, it can
130130+ use it to obtain the access token.
131131+132132+ Figure 1 shows the attack graphically. In step (1), the native
133133+ application running on the end device, such as a smartphone, issues
134134+ an OAuth 2.0 Authorization Request via the browser/operating system.
135135+ The Redirection Endpoint URI in this case typically uses a custom URI
136136+ scheme. Step (1) happens through a secure API that cannot be
137137+ intercepted, though it may potentially be observed in advanced attack
138138+ scenarios. The request then gets forwarded to the OAuth 2.0
139139+ authorization server in step (2). Because OAuth requires the use of
140140+ TLS, this communication is protected by TLS and cannot be
141141+ intercepted. The authorization server returns the authorization code
142142+ in step (3). In step (4), the Authorization Code is returned to the
143143+ requester via the Redirection Endpoint URI that was provided in step
144144+ (1).
145145+146146+ Note that it is possible for a malicious app to register itself as a
147147+ handler for the custom scheme in addition to the legitimate OAuth 2.0
148148+ app. Once it does so, the malicious app is now able to intercept the
149149+ authorization code in step (4). This allows the attacker to request
150150+ and obtain an access token in steps (5) and (6), respectively.
151151+152152+153153+154154+155155+156156+157157+158158+159159+160160+161161+162162+163163+164164+165165+166166+167167+168168+169169+170170+Sakimura, et al. Standards Track [Page 3]
171171+172172+RFC 7636 OAUTH PKCE September 2015
173173+174174+175175+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
176176+ | End Device (e.g., Smartphone) |
177177+ | |
178178+ | +-------------+ +----------+ | (6) Access Token +----------+
179179+ | |Legitimate | | Malicious|<--------------------| |
180180+ | |OAuth 2.0 App| | App |-------------------->| |
181181+ | +-------------+ +----------+ | (5) Authorization | |
182182+ | | ^ ^ | Grant | |
183183+ | | \ | | | |
184184+ | | \ (4) | | | |
185185+ | (1) | \ Authz| | | |
186186+ | Authz| \ Code | | | Authz |
187187+ | Request| \ | | | Server |
188188+ | | \ | | | |
189189+ | | \ | | | |
190190+ | v \ | | | |
191191+ | +----------------------------+ | | |
192192+ | | | | (3) Authz Code | |
193193+ | | Operating System/ |<--------------------| |
194194+ | | Browser |-------------------->| |
195195+ | | | | (2) Authz Request | |
196196+ | +----------------------------+ | +----------+
197197+ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
198198+199199+ Figure 1: Authorization Code Interception Attack
200200+201201+ A number of pre-conditions need to hold for this attack to work:
202202+203203+ 1. The attacker manages to register a malicious application on the
204204+ client device and registers a custom URI scheme that is also used
205205+ by another application. The operating systems must allow a custom
206206+ URI scheme to be registered by multiple applications.
207207+208208+ 2. The OAuth 2.0 authorization code grant is used.
209209+210210+ 3. The attacker has access to the OAuth 2.0 [RFC6749] "client_id" and
211211+ "client_secret" (if provisioned). All OAuth 2.0 native app
212212+ client-instances use the same "client_id". Secrets provisioned in
213213+ client binary applications cannot be considered confidential.
214214+215215+ 4. Either one of the following condition is met:
216216+217217+ 4a. The attacker (via the installed application) is able to
218218+ observe only the responses from the authorization endpoint.
219219+ When "code_challenge_method" value is "plain", only this
220220+ attack is mitigated.
221221+222222+223223+224224+225225+226226+Sakimura, et al. Standards Track [Page 4]
227227+228228+RFC 7636 OAUTH PKCE September 2015
229229+230230+231231+ 4b. A more sophisticated attack scenario allows the attacker to
232232+ observe requests (in addition to responses) to the
233233+ authorization endpoint. The attacker is, however, not able to
234234+ act as a man in the middle. This was caused by leaking http
235235+ log information in the OS. To mitigate this,
236236+ "code_challenge_method" value must be set either to "S256" or
237237+ a value defined by a cryptographically secure
238238+ "code_challenge_method" extension.
239239+240240+ While this is a long list of pre-conditions, the described attack has
241241+ been observed in the wild and has to be considered in OAuth 2.0
242242+ deployments. While the OAuth 2.0 threat model (Section 4.4.1 of
243243+ [RFC6819]) describes mitigation techniques, they are, unfortunately,
244244+ not applicable since they rely on a per-client instance secret or a
245245+ per-client instance redirect URI.
246246+247247+ To mitigate this attack, this extension utilizes a dynamically
248248+ created cryptographically random key called "code verifier". A
249249+ unique code verifier is created for every authorization request, and
250250+ its transformed value, called "code challenge", is sent to the
251251+ authorization server to obtain the authorization code. The
252252+ authorization code obtained is then sent to the token endpoint with
253253+ the "code verifier", and the server compares it with the previously
254254+ received request code so that it can perform the proof of possession
255255+ of the "code verifier" by the client. This works as the mitigation
256256+ since the attacker would not know this one-time key, since it is sent
257257+ over TLS and cannot be intercepted.
258258+259259+1.1. Protocol Flow
260260+261261+ +-------------------+
262262+ | Authz Server |
263263+ +--------+ | +---------------+ |
264264+ | |--(A)- Authorization Request ---->| | |
265265+ | | + t(code_verifier), t_m | | Authorization | |
266266+ | | | | Endpoint | |
267267+ | |<-(B)---- Authorization Code -----| | |
268268+ | | | +---------------+ |
269269+ | Client | | |
270270+ | | | +---------------+ |
271271+ | |--(C)-- Access Token Request ---->| | |
272272+ | | + code_verifier | | Token | |
273273+ | | | | Endpoint | |
274274+ | |<-(D)------ Access Token ---------| | |
275275+ +--------+ | +---------------+ |
276276+ +-------------------+
277277+278278+ Figure 2: Abstract Protocol Flow
279279+280280+281281+282282+Sakimura, et al. Standards Track [Page 5]
283283+284284+RFC 7636 OAUTH PKCE September 2015
285285+286286+287287+ This specification adds additional parameters to the OAuth 2.0
288288+ Authorization and Access Token Requests, shown in abstract form in
289289+ Figure 2.
290290+291291+ A. The client creates and records a secret named the "code_verifier"
292292+ and derives a transformed version "t(code_verifier)" (referred to
293293+ as the "code_challenge"), which is sent in the OAuth 2.0
294294+ Authorization Request along with the transformation method "t_m".
295295+296296+ B. The Authorization Endpoint responds as usual but records
297297+ "t(code_verifier)" and the transformation method.
298298+299299+ C. The client then sends the authorization code in the Access Token
300300+ Request as usual but includes the "code_verifier" secret generated
301301+ at (A).
302302+303303+ D. The authorization server transforms "code_verifier" and compares
304304+ it to "t(code_verifier)" from (B). Access is denied if they are
305305+ not equal.
306306+307307+ An attacker who intercepts the authorization code at (B) is unable to
308308+ redeem it for an access token, as they are not in possession of the
309309+ "code_verifier" secret.
310310+311311+2. Notational Conventions
312312+313313+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
314314+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
315315+ "OPTIONAL" in this document are to be interpreted as described in
316316+ "Key words for use in RFCs to Indicate Requirement Levels" [RFC2119].
317317+ If these words are used without being spelled in uppercase, then they
318318+ are to be interpreted with their natural language meanings.
319319+320320+ This specification uses the Augmented Backus-Naur Form (ABNF)
321321+ notation of [RFC5234].
322322+323323+ STRING denotes a sequence of zero or more ASCII [RFC20] characters.
324324+325325+ OCTETS denotes a sequence of zero or more octets.
326326+327327+ ASCII(STRING) denotes the octets of the ASCII [RFC20] representation
328328+ of STRING where STRING is a sequence of zero or more ASCII
329329+ characters.
330330+331331+ BASE64URL-ENCODE(OCTETS) denotes the base64url encoding of OCTETS,
332332+ per Appendix A, producing a STRING.
333333+334334+335335+336336+337337+338338+Sakimura, et al. Standards Track [Page 6]
339339+340340+RFC 7636 OAUTH PKCE September 2015
341341+342342+343343+ BASE64URL-DECODE(STRING) denotes the base64url decoding of STRING,
344344+ per Appendix A, producing a sequence of octets.
345345+346346+ SHA256(OCTETS) denotes a SHA2 256-bit hash [RFC6234] of OCTETS.
347347+348348+3. Terminology
349349+350350+ In addition to the terms defined in OAuth 2.0 [RFC6749], this
351351+ specification defines the following terms:
352352+353353+ code verifier
354354+ A cryptographically random string that is used to correlate the
355355+ authorization request to the token request.
356356+357357+ code challenge
358358+ A challenge derived from the code verifier that is sent in the
359359+ authorization request, to be verified against later.
360360+361361+ code challenge method
362362+ A method that was used to derive code challenge.
363363+364364+ Base64url Encoding
365365+ Base64 encoding using the URL- and filename-safe character set
366366+ defined in Section 5 of [RFC4648], with all trailing '='
367367+ characters omitted (as permitted by Section 3.2 of [RFC4648]) and
368368+ without the inclusion of any line breaks, whitespace, or other
369369+ additional characters. (See Appendix A for notes on implementing
370370+ base64url encoding without padding.)
371371+372372+3.1. Abbreviations
373373+374374+ ABNF Augmented Backus-Naur Form
375375+376376+ Authz Authorization
377377+378378+ PKCE Proof Key for Code Exchange
379379+380380+ MITM Man-in-the-middle
381381+382382+ MTI Mandatory To Implement
383383+384384+385385+386386+387387+388388+389389+390390+391391+392392+393393+394394+Sakimura, et al. Standards Track [Page 7]
395395+396396+RFC 7636 OAUTH PKCE September 2015
397397+398398+399399+4. Protocol
400400+401401+4.1. Client Creates a Code Verifier
402402+403403+ The client first creates a code verifier, "code_verifier", for each
404404+ OAuth 2.0 [RFC6749] Authorization Request, in the following manner:
405405+406406+ code_verifier = high-entropy cryptographic random STRING using the
407407+ unreserved characters [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / "~"
408408+ from Section 2.3 of [RFC3986], with a minimum length of 43 characters
409409+ and a maximum length of 128 characters.
410410+411411+ ABNF for "code_verifier" is as follows.
412412+413413+ code-verifier = 43*128unreserved
414414+ unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
415415+ ALPHA = %x41-5A / %x61-7A
416416+ DIGIT = %x30-39
417417+418418+ NOTE: The code verifier SHOULD have enough entropy to make it
419419+ impractical to guess the value. It is RECOMMENDED that the output of
420420+ a suitable random number generator be used to create a 32-octet
421421+ sequence. The octet sequence is then base64url-encoded to produce a
422422+ 43-octet URL safe string to use as the code verifier.
423423+424424+4.2. Client Creates the Code Challenge
425425+426426+ The client then creates a code challenge derived from the code
427427+ verifier by using one of the following transformations on the code
428428+ verifier:
429429+430430+ plain
431431+ code_challenge = code_verifier
432432+433433+ S256
434434+ code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
435435+436436+ If the client is capable of using "S256", it MUST use "S256", as
437437+ "S256" is Mandatory To Implement (MTI) on the server. Clients are
438438+ permitted to use "plain" only if they cannot support "S256" for some
439439+ technical reason and know via out-of-band configuration that the
440440+ server supports "plain".
441441+442442+ The plain transformation is for compatibility with existing
443443+ deployments and for constrained environments that can't use the S256
444444+ transformation.
445445+446446+447447+448448+449449+450450+Sakimura, et al. Standards Track [Page 8]
451451+452452+RFC 7636 OAUTH PKCE September 2015
453453+454454+455455+ ABNF for "code_challenge" is as follows.
456456+457457+ code-challenge = 43*128unreserved
458458+ unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
459459+ ALPHA = %x41-5A / %x61-7A
460460+ DIGIT = %x30-39
461461+462462+4.3. Client Sends the Code Challenge with the Authorization Request
463463+464464+ The client sends the code challenge as part of the OAuth 2.0
465465+ Authorization Request (Section 4.1.1 of [RFC6749]) using the
466466+ following additional parameters:
467467+468468+ code_challenge
469469+ REQUIRED. Code challenge.
470470+471471+ code_challenge_method
472472+ OPTIONAL, defaults to "plain" if not present in the request. Code
473473+ verifier transformation method is "S256" or "plain".
474474+475475+4.4. Server Returns the Code
476476+477477+ When the server issues the authorization code in the authorization
478478+ response, it MUST associate the "code_challenge" and
479479+ "code_challenge_method" values with the authorization code so it can
480480+ be verified later.
481481+482482+ Typically, the "code_challenge" and "code_challenge_method" values
483483+ are stored in encrypted form in the "code" itself but could
484484+ alternatively be stored on the server associated with the code. The
485485+ server MUST NOT include the "code_challenge" value in client requests
486486+ in a form that other entities can extract.
487487+488488+ The exact method that the server uses to associate the
489489+ "code_challenge" with the issued "code" is out of scope for this
490490+ specification.
491491+492492+4.4.1. Error Response
493493+494494+ If the server requires Proof Key for Code Exchange (PKCE) by OAuth
495495+ public clients and the client does not send the "code_challenge" in
496496+ the request, the authorization endpoint MUST return the authorization
497497+ error response with the "error" value set to "invalid_request". The
498498+ "error_description" or the response of "error_uri" SHOULD explain the
499499+ nature of error, e.g., code challenge required.
500500+501501+502502+503503+504504+505505+506506+Sakimura, et al. Standards Track [Page 9]
507507+508508+RFC 7636 OAUTH PKCE September 2015
509509+510510+511511+ If the server supporting PKCE does not support the requested
512512+ transformation, the authorization endpoint MUST return the
513513+ authorization error response with "error" value set to
514514+ "invalid_request". The "error_description" or the response of
515515+ "error_uri" SHOULD explain the nature of error, e.g., transform
516516+ algorithm not supported.
517517+518518+4.5. Client Sends the Authorization Code and the Code Verifier to the
519519+ Token Endpoint
520520+521521+ Upon receipt of the Authorization Code, the client sends the Access
522522+ Token Request to the token endpoint. In addition to the parameters
523523+ defined in the OAuth 2.0 Access Token Request (Section 4.1.3 of
524524+ [RFC6749]), it sends the following parameter:
525525+526526+ code_verifier
527527+ REQUIRED. Code verifier
528528+529529+ The "code_challenge_method" is bound to the Authorization Code when
530530+ the Authorization Code is issued. That is the method that the token
531531+ endpoint MUST use to verify the "code_verifier".
532532+533533+4.6. Server Verifies code_verifier before Returning the Tokens
534534+535535+ Upon receipt of the request at the token endpoint, the server
536536+ verifies it by calculating the code challenge from the received
537537+ "code_verifier" and comparing it with the previously associated
538538+ "code_challenge", after first transforming it according to the
539539+ "code_challenge_method" method specified by the client.
540540+541541+ If the "code_challenge_method" from Section 4.3 was "S256", the
542542+ received "code_verifier" is hashed by SHA-256, base64url-encoded, and
543543+ then compared to the "code_challenge", i.e.:
544544+545545+ BASE64URL-ENCODE(SHA256(ASCII(code_verifier))) == code_challenge
546546+547547+ If the "code_challenge_method" from Section 4.3 was "plain", they are
548548+ compared directly, i.e.:
549549+550550+ code_verifier == code_challenge.
551551+552552+ If the values are equal, the token endpoint MUST continue processing
553553+ as normal (as defined by OAuth 2.0 [RFC6749]). If the values are not
554554+ equal, an error response indicating "invalid_grant" as described in
555555+ Section 5.2 of [RFC6749] MUST be returned.
556556+557557+558558+559559+560560+561561+562562+Sakimura, et al. Standards Track [Page 10]
563563+564564+RFC 7636 OAUTH PKCE September 2015
565565+566566+567567+5. Compatibility
568568+569569+ Server implementations of this specification MAY accept OAuth2.0
570570+ clients that do not implement this extension. If the "code_verifier"
571571+ is not received from the client in the Authorization Request, servers
572572+ supporting backwards compatibility revert to the OAuth 2.0 [RFC6749]
573573+ protocol without this extension.
574574+575575+ As the OAuth 2.0 [RFC6749] server responses are unchanged by this
576576+ specification, client implementations of this specification do not
577577+ need to know if the server has implemented this specification or not
578578+ and SHOULD send the additional parameters as defined in Section 4 to
579579+ all servers.
580580+581581+6. IANA Considerations
582582+583583+ IANA has made the following registrations per this document.
584584+585585+6.1. OAuth Parameters Registry
586586+587587+ This specification registers the following parameters in the IANA
588588+ "OAuth Parameters" registry defined in OAuth 2.0 [RFC6749].
589589+590590+ o Parameter name: code_verifier
591591+ o Parameter usage location: token request
592592+ o Change controller: IESG
593593+ o Specification document(s): RFC 7636 (this document)
594594+595595+ o Parameter name: code_challenge
596596+ o Parameter usage location: authorization request
597597+ o Change controller: IESG
598598+ o Specification document(s): RFC 7636 (this document)
599599+600600+ o Parameter name: code_challenge_method
601601+ o Parameter usage location: authorization request
602602+ o Change controller: IESG
603603+ o Specification document(s): RFC 7636 (this document)
604604+605605+6.2. PKCE Code Challenge Method Registry
606606+607607+ This specification establishes the "PKCE Code Challenge Methods"
608608+ registry. The new registry should be a sub-registry of the "OAuth
609609+ Parameters" registry.
610610+611611+ Additional "code_challenge_method" types for use with the
612612+ authorization endpoint are registered using the Specification
613613+ Required policy [RFC5226], which includes review of the request by
614614+ one or more Designated Experts (DEs). The DEs will ensure that there
615615+616616+617617+618618+Sakimura, et al. Standards Track [Page 11]
619619+620620+RFC 7636 OAUTH PKCE September 2015
621621+622622+623623+ is at least a two-week review of the request on the oauth-ext-
624624+ review@ietf.org mailing list and that any discussion on that list
625625+ converges before they respond to the request. To allow for the
626626+ allocation of values prior to publication, the Designated Expert(s)
627627+ may approve registration once they are satisfied that an acceptable
628628+ specification will be published.
629629+630630+ Registration requests and discussion on the oauth-ext-review@ietf.org
631631+ mailing list should use an appropriate subject, such as "Request for
632632+ PKCE code_challenge_method: example").
633633+634634+ The Designated Expert(s) should consider the discussion on the
635635+ mailing list, as well as the overall security properties of the
636636+ challenge method when evaluating registration requests. New methods
637637+ should not disclose the value of the code_verifier in the request to
638638+ the Authorization endpoint. Denials should include an explanation
639639+ and, if applicable, suggestions as to how to make the request
640640+ successful.
641641+642642+6.2.1. Registration Template
643643+644644+ Code Challenge Method Parameter Name:
645645+ The name requested (e.g., "example"). Because a core goal of this
646646+ specification is for the resulting representations to be compact,
647647+ it is RECOMMENDED that the name be short -- not to exceed 8
648648+ characters without a compelling reason to do so. This name is
649649+ case-sensitive. Names may not match other registered names in a
650650+ case-insensitive manner unless the Designated Expert(s) states
651651+ that there is a compelling reason to allow an exception in this
652652+ particular case.
653653+654654+ Change Controller:
655655+ For Standards Track RFCs, state "IESG". For others, give the name
656656+ of the responsible party. Other details (e.g., postal address,
657657+ email address, and home page URI) may also be included.
658658+659659+ Specification Document(s):
660660+ Reference to the document(s) that specifies the parameter,
661661+ preferably including URI(s) that can be used to retrieve copies of
662662+ the document(s). An indication of the relevant sections may also
663663+ be included but is not required.
664664+665665+666666+667667+668668+669669+670670+671671+672672+673673+674674+Sakimura, et al. Standards Track [Page 12]
675675+676676+RFC 7636 OAUTH PKCE September 2015
677677+678678+679679+6.2.2. Initial Registry Contents
680680+681681+ Per this document, IANA has registered the Code Challenge Method
682682+ Parameter Names defined in Section 4.2 in this registry.
683683+684684+ o Code Challenge Method Parameter Name: plain
685685+ o Change Controller: IESG
686686+ o Specification Document(s): Section 4.2 of RFC 7636 (this document)
687687+688688+ o Code Challenge Method Parameter Name: S256
689689+ o Change Controller: IESG
690690+ o Specification Document(s): Section 4.2 of RFC 7636 (this document)
691691+692692+7. Security Considerations
693693+694694+7.1. Entropy of the code_verifier
695695+696696+ The security model relies on the fact that the code verifier is not
697697+ learned or guessed by the attacker. It is vitally important to
698698+ adhere to this principle. As such, the code verifier has to be
699699+ created in such a manner that it is cryptographically random and has
700700+ high entropy that it is not practical for the attacker to guess.
701701+702702+ The client SHOULD create a "code_verifier" with a minimum of 256 bits
703703+ of entropy. This can be done by having a suitable random number
704704+ generator create a 32-octet sequence. The octet sequence can then be
705705+ base64url-encoded to produce a 43-octet URL safe string to use as a
706706+ "code_challenge" that has the required entropy.
707707+708708+7.2. Protection against Eavesdroppers
709709+710710+ Clients MUST NOT downgrade to "plain" after trying the "S256" method.
711711+ Servers that support PKCE are required to support "S256", and servers
712712+ that do not support PKCE will simply ignore the unknown
713713+ "code_verifier". Because of this, an error when "S256" is presented
714714+ can only mean that the server is faulty or that a MITM attacker is
715715+ trying a downgrade attack.
716716+717717+ The "S256" method protects against eavesdroppers observing or
718718+ intercepting the "code_challenge", because the challenge cannot be
719719+ used without the verifier. With the "plain" method, there is a
720720+ chance that "code_challenge" will be observed by the attacker on the
721721+ device or in the http request. Since the code challenge is the same
722722+ as the code verifier in this case, the "plain" method does not
723723+ protect against the eavesdropping of the initial request.
724724+725725+ The use of "S256" protects against disclosure of the "code_verifier"
726726+ value to an attacker.
727727+728728+729729+730730+Sakimura, et al. Standards Track [Page 13]
731731+732732+RFC 7636 OAUTH PKCE September 2015
733733+734734+735735+ Because of this, "plain" SHOULD NOT be used and exists only for
736736+ compatibility with deployed implementations where the request path is
737737+ already protected. The "plain" method SHOULD NOT be used in new
738738+ implementations, unless they cannot support "S256" for some technical
739739+ reason.
740740+741741+ The "S256" code challenge method or other cryptographically secure
742742+ code challenge method extension SHOULD be used. The "plain" code
743743+ challenge method relies on the operating system and transport
744744+ security not to disclose the request to an attacker.
745745+746746+ If the code challenge method is "plain" and the code challenge is to
747747+ be returned inside authorization "code" to achieve a stateless
748748+ server, it MUST be encrypted in such a manner that only the server
749749+ can decrypt and extract it.
750750+751751+7.3. Salting the code_challenge
752752+753753+ To reduce implementation complexity, salting is not used in the
754754+ production of the code challenge, as the code verifier contains
755755+ sufficient entropy to prevent brute-force attacks. Concatenating a
756756+ publicly known value to a code verifier (containing 256 bits of
757757+ entropy) and then hashing it with SHA256 to produce a code challenge
758758+ would not increase the number of attempts necessary to brute force a
759759+ valid value for code verifier.
760760+761761+ While the "S256" transformation is like hashing a password, there are
762762+ important differences. Passwords tend to be relatively low-entropy
763763+ words that can be hashed offline and the hash looked up in a
764764+ dictionary. By concatenating a unique though public value to each
765765+ password prior to hashing, the dictionary space that an attacker
766766+ needs to search is greatly expanded.
767767+768768+ Modern graphics processors now allow attackers to calculate hashes in
769769+ real time faster than they could be looked up from a disk. This
770770+ eliminates the value of the salt in increasing the complexity of a
771771+ brute-force attack for even low-entropy passwords.
772772+773773+7.4. OAuth Security Considerations
774774+775775+ All the OAuth security analysis presented in [RFC6819] applies, so
776776+ readers SHOULD carefully follow it.
777777+778778+779779+780780+781781+782782+783783+784784+785785+786786+Sakimura, et al. Standards Track [Page 14]
787787+788788+RFC 7636 OAUTH PKCE September 2015
789789+790790+791791+7.5. TLS Security Considerations
792792+793793+ Current security considerations can be found in "Recommendations for
794794+ Secure Use of Transport Layer Security (TLS) and Datagram Transport
795795+ Layer Security (DTLS)" [BCP195]. This supersedes the TLS version
796796+ recommendations in OAuth 2.0 [RFC6749].
797797+798798+8. References
799799+800800+8.1. Normative References
801801+802802+ [BCP195] Sheffer, Y., Holz, R., and P. Saint-Andre,
803803+ "Recommendations for Secure Use of Transport Layer
804804+ Security (TLS) and Datagram Transport Layer Security
805805+ (DTLS)", BCP 195, RFC 7525, May 2015,
806806+ <http://www.rfc-editor.org/info/bcp195>.
807807+808808+ [RFC20] Cerf, V., "ASCII format for network interchange", STD 80,
809809+ RFC 20, DOI 10.17487/RFC0020, October 1969,
810810+ <http://www.rfc-editor.org/info/rfc20>.
811811+812812+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
813813+ Requirement Levels", BCP 14, RFC 2119,
814814+ DOI 10.17487/RFC2119, March 1997,
815815+ <http://www.rfc-editor.org/info/rfc2119>.
816816+817817+ [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform
818818+ Resource Identifier (URI): Generic Syntax", STD 66, RFC
819819+ 3986, DOI 10.17487/RFC3986, January 2005,
820820+ <http://www.rfc-editor.org/info/rfc3986>.
821821+822822+ [RFC4648] Josefsson, S., "The Base16, Base32, and Base64 Data
823823+ Encodings", RFC 4648, DOI 10.17487/RFC4648, October 2006,
824824+ <http://www.rfc-editor.org/info/rfc4648>.
825825+826826+ [RFC5226] Narten, T. and H. Alvestrand, "Guidelines for Writing an
827827+ IANA Considerations Section in RFCs", BCP 26, RFC 5226,
828828+ DOI 10.17487/RFC5226, May 2008,
829829+ <http://www.rfc-editor.org/info/rfc5226>.
830830+831831+ [RFC5234] Crocker, D., Ed. and P. Overell, "Augmented BNF for Syntax
832832+ Specifications: ABNF", STD 68, RFC 5234,
833833+ DOI 10.17487/RFC5234, January 2008,
834834+ <http://www.rfc-editor.org/info/rfc5234>.
835835+836836+837837+838838+839839+840840+841841+842842+Sakimura, et al. Standards Track [Page 15]
843843+844844+RFC 7636 OAUTH PKCE September 2015
845845+846846+847847+ [RFC6234] Eastlake 3rd, D. and T. Hansen, "US Secure Hash Algorithms
848848+ (SHA and SHA-based HMAC and HKDF)", RFC 6234,
849849+ DOI 10.17487/RFC6234, May 2011,
850850+ <http://www.rfc-editor.org/info/rfc6234>.
851851+852852+ [RFC6749] Hardt, D., Ed., "The OAuth 2.0 Authorization Framework",
853853+ RFC 6749, DOI 10.17487/RFC6749, October 2012,
854854+ <http://www.rfc-editor.org/info/rfc6749>.
855855+856856+8.2. Informative References
857857+858858+ [RFC6819] Lodderstedt, T., Ed., McGloin, M., and P. Hunt, "OAuth 2.0
859859+ Threat Model and Security Considerations", RFC 6819,
860860+ DOI 10.17487/RFC6819, January 2013,
861861+ <http://www.rfc-editor.org/info/rfc6819>.
862862+863863+864864+865865+866866+867867+868868+869869+870870+871871+872872+873873+874874+875875+876876+877877+878878+879879+880880+881881+882882+883883+884884+885885+886886+887887+888888+889889+890890+891891+892892+893893+894894+895895+896896+897897+898898+Sakimura, et al. Standards Track [Page 16]
899899+900900+RFC 7636 OAUTH PKCE September 2015
901901+902902+903903+Appendix A. Notes on Implementing Base64url Encoding without Padding
904904+905905+ This appendix describes how to implement a base64url-encoding
906906+ function without padding, based upon the standard base64-encoding
907907+ function that uses padding.
908908+909909+ To be concrete, example C# code implementing these functions is shown
910910+ below. Similar code could be used in other languages.
911911+912912+ static string base64urlencode(byte [] arg)
913913+ {
914914+ string s = Convert.ToBase64String(arg); // Regular base64 encoder
915915+ s = s.Split('=')[0]; // Remove any trailing '='s
916916+ s = s.Replace('+', '-'); // 62nd char of encoding
917917+ s = s.Replace('/', '_'); // 63rd char of encoding
918918+ return s;
919919+ }
920920+921921+ An example correspondence between unencoded and encoded values
922922+ follows. The octet sequence below encodes into the string below,
923923+ which when decoded, reproduces the octet sequence.
924924+925925+ 3 236 255 224 193
926926+927927+ A-z_4ME
928928+929929+Appendix B. Example for the S256 code_challenge_method
930930+931931+ The client uses output of a suitable random number generator to
932932+ create a 32-octet sequence. The octets representing the value in
933933+ this example (using JSON array notation) are:
934934+935935+ [116, 24, 223, 180, 151, 153, 224, 37, 79, 250, 96, 125, 216, 173,
936936+ 187, 186, 22, 212, 37, 77, 105, 214, 191, 240, 91, 88, 5, 88, 83,
937937+ 132, 141, 121]
938938+939939+ Encoding this octet sequence as base64url provides the value of the
940940+ code_verifier:
941941+942942+ dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
943943+944944+ The code_verifier is then hashed via the SHA256 hash function to
945945+ produce:
946946+947947+ [19, 211, 30, 150, 26, 26, 216, 236, 47, 22, 177, 12, 76, 152, 46,
948948+ 8, 118, 168, 120, 173, 109, 241, 68, 86, 110, 225, 137, 74, 203,
949949+ 112, 249, 195]
950950+951951+952952+953953+954954+Sakimura, et al. Standards Track [Page 17]
955955+956956+RFC 7636 OAUTH PKCE September 2015
957957+958958+959959+ Encoding this octet sequence as base64url provides the value of the
960960+ code_challenge:
961961+962962+ E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
963963+964964+ The authorization request includes:
965965+966966+ code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
967967+ &code_challenge_method=S256
968968+969969+ The authorization server then records the code_challenge and
970970+ code_challenge_method along with the code that is granted to the
971971+ client.
972972+973973+ In the request to the token_endpoint, the client includes the code
974974+ received in the authorization response as well as the additional
975975+ parameter:
976976+977977+ code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk
978978+979979+ The authorization server retrieves the information for the code
980980+ grant. Based on the recorded code_challenge_method being S256, it
981981+ then hashes and base64url-encodes the value of code_verifier:
982982+983983+ BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
984984+985985+ The calculated value is then compared with the value of
986986+ "code_challenge":
987987+988988+ BASE64URL-ENCODE(SHA256(ASCII(code_verifier))) == code_challenge
989989+990990+ If the two values are equal, then the authorization server can
991991+ provide the tokens as long as there are no other errors in the
992992+ request. If the values are not equal, then the request must be
993993+ rejected, and an error returned.
994994+995995+996996+997997+998998+999999+10001000+10011001+10021002+10031003+10041004+10051005+10061006+10071007+10081008+10091009+10101010+Sakimura, et al. Standards Track [Page 18]
10111011+10121012+RFC 7636 OAUTH PKCE September 2015
10131013+10141014+10151015+Acknowledgements
10161016+10171017+ The initial draft version of this specification was created by the
10181018+ OpenID AB/Connect Working Group of the OpenID Foundation.
10191019+10201020+ This specification is the work of the OAuth Working Group, which
10211021+ includes dozens of active and dedicated participants. In particular,
10221022+ the following individuals contributed ideas, feedback, and wording
10231023+ that shaped and formed the final specification:
10241024+10251025+ Anthony Nadalin, Microsoft
10261026+ Axel Nenker, Deutsche Telekom
10271027+ Breno de Medeiros, Google
10281028+ Brian Campbell, Ping Identity
10291029+ Chuck Mortimore, Salesforce
10301030+ Dirk Balfanz, Google
10311031+ Eduardo Gueiros, Jive Communications
10321032+ Hannes Tschonfenig, ARM
10331033+ James Manger, Telstra
10341034+ Justin Richer, MIT Kerberos
10351035+ Josh Mandel, Boston Children's Hospital
10361036+ Lewis Adam, Motorola Solutions
10371037+ Madjid Nakhjiri, Samsung
10381038+ Michael B. Jones, Microsoft
10391039+ Paul Madsen, Ping Identity
10401040+ Phil Hunt, Oracle
10411041+ Prateek Mishra, Oracle
10421042+ Ryo Ito, mixi
10431043+ Scott Tomilson, Ping Identity
10441044+ Sergey Beryozkin
10451045+ Takamichi Saito
10461046+ Torsten Lodderstedt, Deutsche Telekom
10471047+ William Denniss, Google
10481048+10491049+10501050+10511051+10521052+10531053+10541054+10551055+10561056+10571057+10581058+10591059+10601060+10611061+10621062+10631063+10641064+10651065+10661066+Sakimura, et al. Standards Track [Page 19]
10671067+10681068+RFC 7636 OAUTH PKCE September 2015
10691069+10701070+10711071+Authors' Addresses
10721072+10731073+ Nat Sakimura (editor)
10741074+ Nomura Research Institute
10751075+ 1-6-5 Marunouchi, Marunouchi Kitaguchi Bldg.
10761076+ Chiyoda-ku, Tokyo 100-0005
10771077+ Japan
10781078+10791079+ Phone: +81-3-5533-2111
10801080+ Email: n-sakimura@nri.co.jp
10811081+ URI: http://nat.sakimura.org/
10821082+10831083+10841084+ John Bradley
10851085+ Ping Identity
10861086+ Casilla 177, Sucursal Talagante
10871087+ Talagante, RM
10881088+ Chile
10891089+10901090+ Phone: +44 20 8133 3718
10911091+ Email: ve7jtb@ve7jtb.com
10921092+ URI: http://www.thread-safe.com/
10931093+10941094+10951095+ Naveen Agarwal
10961096+ Google
10971097+ 1600 Amphitheatre Parkway
10981098+ Mountain View, CA 94043
10991099+ United States
11001100+11011101+ Phone: +1 650-253-0000
11021102+ Email: naa@google.com
11031103+ URI: http://google.com/
11041104+11051105+11061106+11071107+11081108+11091109+11101110+11111111+11121112+11131113+11141114+11151115+11161116+11171117+11181118+11191119+11201120+11211121+11221122+Sakimura, et al. Standards Track [Page 20]
11231123+