From 484b2c4575137b4f71f8e0260a1d417c0e489bc9 Mon Sep 17 00:00:00 2001 From: Johannes Blaschke Date: Mon, 23 Oct 2023 12:42:50 +0200 Subject: [PATCH 1/4] fix typo --- examples/mux_example.jl | 4 ++-- examples/oxygen_example.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/mux_example.jl b/examples/mux_example.jl index 76e3a62..1bace80 100644 --- a/examples/mux_example.jl +++ b/examples/mux_example.jl @@ -44,8 +44,8 @@ end page("/", respond("Authenticate with Google")), page(oauth_path, req -> oauth2.redirect()), page(oauth_callback, callback), - page("/protected", respond("Congrets, You signed in Successfully!")), + page("/protected", respond("Congrats, You signed in Successfully!")), Mux.notfound() ) -serve(http, 3000) \ No newline at end of file +serve(http, 3000) diff --git a/examples/oxygen_example.jl b/examples/oxygen_example.jl index 422cd1f..701d35c 100644 --- a/examples/oxygen_example.jl +++ b/examples/oxygen_example.jl @@ -40,8 +40,8 @@ end end @get "/protected" function() - "Congrets, You signed in Successfully!" + "Congrats, You signed in Successfully!" end # start the web server -serve() \ No newline at end of file +serve() From da8daf779abba19e19c3694a50bb7ef34df8c60b Mon Sep 17 00:00:00 2001 From: Johannes Blaschke Date: Mon, 23 Oct 2023 13:06:40 +0200 Subject: [PATCH 2/4] add parameters to redirect following successful token exchange --- Project.toml | 2 +- src/initiator.jl | 8 ++++---- src/utils.jl | 25 +++++++++++++++++++------ test/app.jl | 5 +++-- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/Project.toml b/Project.toml index ce11711..6d698a4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Umbrella" uuid = "fb3c7edd-4d33-4ece-bf81-49ff2d418a94" authors = ["Jiacheng Zhang "] -version = "0.2.0" +version = "0.3.0" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" diff --git a/src/initiator.jl b/src/initiator.jl index c506a50..87f0854 100644 --- a/src/initiator.jl +++ b/src/initiator.jl @@ -59,15 +59,15 @@ function init(type::Symbol, config::Configuration.Options, redirect_hanlder::Fun type, function () url = actions[:redirect](config) - redirect_hanlder(url) + redirect_hanlder(url, nothing) end, function (code::String, verify::Function) tokens, profile = actions[:token_exchange](code, config) if tokens === nothing || profile === nothing - return redirect_hanlder(config.failure_redirect) + return redirect_hanlder(config.failure_redirect, nothing) end - verify(tokens, profile) - redirect_hanlder(config.success_redirect) + params = verify(tokens, profile) + redirect_hanlder(config.success_redirect, params) end ) end diff --git a/src/utils.jl b/src/utils.jl index 7ccbbf2..ba73fb6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,9 +1,22 @@ import HTTP -function redirect(url::String, status::Int = 302) - headers = Dict{String, String}( - "Location" => url - ) +function redirect( + url::String, params::Dict{String, Any}=nothing, + status::Int = 302 + ) - HTTP.Response(status, [h for h in headers]) -end \ No newline at end of file + # Set redirect parameters (if they exist) + if ! isnothing(params) + uri = HTTP.URI( + path = url, + query = params + ) + url = string(uri) + end + + # Redirect to url + headers = Dict{String, String}( + "Location" => url + ) + HTTP.Response(status, [h for h in headers]) +end diff --git a/test/app.jl b/test/app.jl index 35e9bd8..9e54ca3 100644 --- a/test/app.jl +++ b/test/app.jl @@ -1,7 +1,7 @@ const MOCK_AUTH_URL = "https://mock.com/oauth2/" -function mock_redirect(url::String) url end +function mock_redirect(url::String, params::Nothing) url end function mock_redirect_url(config::Configuration.Options) "$(MOCK_AUTH_URL)?client_id=$(config.client_id)&redirect_uri=$(config.redirect_uri)&scope=$(config.scope)" @@ -13,6 +13,7 @@ end function mock_verify(tokens, user) tokens, user + return nothing end register(:mock, OAuth2Actions(mock_redirect_url, mock_token_exchange)) @@ -35,4 +36,4 @@ register(:mock, OAuth2Actions(mock_redirect_url, mock_token_exchange)) @test instance.redirect() == "https://mock.com/oauth2/?client_id=mock_id&redirect_uri=http://localhost:3000/oauth2/callback&scope=profile%20openid%20email" @test instance.token_exchange("test_code", mock_verify) == success_redirect -end \ No newline at end of file +end From 0763fba6d77f153b9ed9392eb73b8ad878674e08 Mon Sep 17 00:00:00 2001 From: Johannes Blaschke Date: Mon, 23 Oct 2023 13:23:18 +0200 Subject: [PATCH 3/4] fix error with multiple dispatch --- src/utils.jl | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index ba73fb6..44fd07a 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,19 +1,13 @@ import HTTP -function redirect( - url::String, params::Dict{String, Any}=nothing, - status::Int = 302 - ) +function redirect(url::String, params::Dict{String, Any}, status::Int = 302) + # Set redirect parameters + uri = HTTP.URI(path = url, query = params) + redirect(string(uri), nothing, status) +end - # Set redirect parameters (if they exist) - if ! isnothing(params) - uri = HTTP.URI( - path = url, - query = params - ) - url = string(uri) - end +function redirect(url::String, ::Nothing, status::Int = 302) # Redirect to url headers = Dict{String, String}( "Location" => url From fcbf21bfe823835737909a1c06ebe9c46eb4e5d4 Mon Sep 17 00:00:00 2001 From: Johannes Blaschke Date: Mon, 23 Oct 2023 13:30:50 +0200 Subject: [PATCH 4/4] more debugging of types --- src/utils.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.jl b/src/utils.jl index 44fd07a..ff70e55 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -1,6 +1,6 @@ import HTTP -function redirect(url::String, params::Dict{String, Any}, status::Int = 302) +function redirect(url::String, params::Dict{String, <:Any}, status::Int = 302) # Set redirect parameters uri = HTTP.URI(path = url, query = params) redirect(string(uri), nothing, status)