Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenient add_params_to_url function #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions src/ur/oauth.ur
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,13 @@ functor MakeDyn(M : sig
dml (INSERT INTO states(State, Expires) VALUES({[state]}, {[addSeconds tm 300]}));
settings <- settings;

redirect (bless (show settings.AuthorizeUrl
^ "?client_id=" ^ urlencode settings.ClientId
^ "&redirect_uri=" ^ urlencode (show (effectfulUrl authorized))
^ "&state=" ^ show state
^ "&response_type=code"
^ (case scope of
None => ""
| Some scope => "&" ^ Option.get "scope" nameForScopeParameter
^ "=" ^ urlencode scope)
^ (case hosted_domain of
None => ""
| Some hd => "&hd=" ^ urlencode hd)))
redirect (add_params_to_url settings.AuthorizeUrl
(("client_id", Some settings.ClientId),
("redirect_uri", Some (show (effectfulUrl authorized))),
("state", Some (show state)),
("response_type", Some "code"),
(Option.get "scope" nameForScopeParameter, scope),
("hd", hosted_domain)))
end
end

Expand Down
10 changes: 10 additions & 0 deletions src/ur/urls.ur
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,13 @@ fun base64url_encode' (getChar : int -> char) (urlVersion : bool) (len : int) =
fun base64url_encode s = base64url_encode' (String.sub s) True (String.length s)
fun base64url_encode_signature s = base64url_encode' (WorldFfi.byte s) True (WorldFfi.length s)
fun base64_encode_signature s = base64url_encode' (WorldFfi.byte s) False (WorldFfi.length s)

fun add_params_to_url [r] fl base_url params =
(* If there's a '?' in the base url, then it already has params, and we continue with '&'. *)
let val initialPrefix = if String.all (fn ch => ch <> #"?") (show base_url) then "?" else "&"
fun foldFun [nm ::_] [rest ::_] [[nm] ~ rest] (nm, op) (prefix, rst) = case op of
None => (prefix, rst)
| Some p => ("&", rst ^ prefix ^ nm ^ "=" ^ urlencode p)
val encoded_params = @foldUR [(string * option string)] [fn _ => (string * string)] foldFun (initialPrefix, "") fl params
in bless (show base_url ^ encoded_params.2)
end
7 changes: 7 additions & 0 deletions src/ur/urls.urs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ val urldecode : string -> string
val base64url_encode : string -> string
val base64url_encode_signature : WorldFfi.signatur -> string
val base64_encode_signature : WorldFfi.signatur -> string

(* Takes a base URL along with a record of name/value pairs and formats the
pairs into the typical "base_url?nm1=val1&nm2=val2...", properly URL encoding
the given strings.
The base URL may already name/value pairs.
If the value is `None`, then the name/value pair is omitted from the result *)
val add_params_to_url : r ::: {Unit} -> folder r -> url -> $(mapU (string * option string) r) -> url