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 parameters to redirect #7

Open
wants to merge 4 commits into
base: main
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Umbrella"
uuid = "fb3c7edd-4d33-4ece-bf81-49ff2d418a94"
authors = ["Jiacheng Zhang <[email protected]>"]
version = "0.2.0"
version = "0.3.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
4 changes: 2 additions & 2 deletions examples/mux_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ end
page("/", respond("<a href='$(oauth_path)'>Authenticate with Google</a>")),
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)
serve(http, 3000)
4 changes: 2 additions & 2 deletions examples/oxygen_example.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
serve()
8 changes: 4 additions & 4 deletions src/initiator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 13 additions & 6 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import HTTP

function redirect(url::String, status::Int = 302)
headers = Dict{String, String}(
"Location" => url
)
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

HTTP.Response(status, [h for h in headers])
end

function redirect(url::String, ::Nothing, status::Int = 302)
# Redirect to url
headers = Dict{String, String}(
"Location" => url
)
HTTP.Response(status, [h for h in headers])
end
5 changes: 3 additions & 2 deletions test/app.jl
Original file line number Diff line number Diff line change
@@ -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)"
Expand All @@ -13,6 +13,7 @@ end

function mock_verify(tokens, user)
tokens, user
return nothing
end

register(:mock, OAuth2Actions(mock_redirect_url, mock_token_exchange))
Expand All @@ -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
end