-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of authorization code grant (#20)
- Loading branch information
Showing
12 changed files
with
897 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package oauth2 | ||
|
||
type Client struct { | ||
clientID string | ||
clientSecret string | ||
ClientID string | ||
|
||
ClientSecret string | ||
|
||
RedirectURI string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package login | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
oauth2 "github.com/oxisto/oauth2go" | ||
) | ||
|
||
// handleAuthorize implements the authorize endpoint (see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1). | ||
func (h *handler) handleAuthorize(w http.ResponseWriter, r *http.Request) { | ||
var ( | ||
client *oauth2.Client | ||
redirectURI string | ||
state string | ||
err error | ||
query url.Values | ||
session *session | ||
) | ||
|
||
query = r.URL.Query() | ||
|
||
client, err = h.srv.GetClient(query.Get("client_id")) | ||
if err != nil { | ||
http.Error(w, "Invalid client ID", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
redirectURI = query.Get("redirect_uri") | ||
if redirectURI == "" || client.RedirectURI != redirectURI { | ||
http.Error(w, "Invalid redirect URI", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if query.Get("response_type") != "code" { | ||
oauth2.RedirectError(w, r, redirectURI, "invalid_request") | ||
return | ||
} | ||
|
||
state = query.Get("state") | ||
|
||
// Check, if we already have a session | ||
session = h.extractSession(w, r) | ||
|
||
if session.Anonymous() { | ||
var params = url.Values{} | ||
params.Add("return_url", r.RequestURI) | ||
|
||
// Redirect to our login page | ||
http.Redirect(w, r, fmt.Sprintf("/login?%s", params.Encode()), http.StatusFound) | ||
} else { | ||
var params = url.Values{} | ||
params.Add("code", h.srv.IssueCode()) | ||
params.Add("state", state) | ||
|
||
http.Redirect(w, r, fmt.Sprintf("%s?%s", redirectURI, params.Encode()), http.StatusFound) | ||
} | ||
} |
Oops, something went wrong.