Skip to content

Added support for OAuth 2.0 State Parameter #2

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

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
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
tmplVal["ClientID"] = r.FormValue("client_id")
tmplVal["ResponseType"] = r.FormValue("response_type")
tmplVal["RedirectURI"] = r.FormValue("redirect_uri")
tmplVal["State"] = r.FormValue("state")

t, _ := template.ParseFiles("tmpl/login.html")
t.Execute(w, tmplVal)
Expand All @@ -26,9 +27,10 @@ func approvedHandler(w http.ResponseWriter, r *http.Request) {
var redirect_uri = r.FormValue("redirect_uri")
var responseType = r.FormValue("response_type")
var clientId = r.FormValue("client_id")
var State = r.FormValue("state")

thisResponse, rErr := RequestOAuthToken(APIlistenPath,
redirect_uri, responseType, clientId, "", orgID, policyID, BaseAPIID)
redirect_uri, responseType, clientId, "", orgID, policyID, BaseAPIID, State)

if rErr != nil {
log.Error(rErr)
Expand Down
1 change: 1 addition & 0 deletions tmpl/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2>1. Third Party Requester (Client)</h2>
<input type="hidden" class="form-control" id="response_type" name="response_type" value="token">
<input type="hidden" class="form-control" id="client_id" name="client_id" value="16076d969bc745ec7018dd4343fac83b">
<input type="hidden" class="form-control" id="redirect_uri" name="redirect_uri" value="http://localhost:8000/final">
<input type="hidden" class="form-control" id="state" name="state" value="1234567890">
<button type="submit" class="btn btn-default">Start OAuth</button>
</form>
</div>
Expand Down
2 changes: 2 additions & 0 deletions tmpl/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ <h2>Login Form (Your identity portal)</h2>
<li>Client ID: {{.ClientID}}</li>
<li>Redirect URL: {{.RedirectURI}}</li>
<li>Response Type: {{.ResponseType}}</li>
<li>State: {{.State}}</li>
</ul>
</p>
<p>
Expand All @@ -39,6 +40,7 @@ <h2>Login Form (Your identity portal)</h2>
<input type="hidden" class="form-control" id="response_type" name="response_type" value="{{.ResponseType}}">
<input type="hidden" class="form-control" id="client_id" name="client_id" value="{{.ClientID}}">
<input type="hidden" class="form-control" id="redirect_uri" name="redirect_uri" value="{{.RedirectURI}}">
<input type="hidden" class="form-control" id="state" name="state" value="{{.State}}">
<button type="submit" class="btn btn-default">Login</button>
</form>
</div>
Expand Down
3 changes: 2 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func DispatchGateway(target Endpoint, method string, body io.Reader, ctype strin
return retBody, nil
}

func RequestOAuthToken(APIlistenPath, redirect_uri, responseType, clientId, secret, orgID, policyID, BaseAPIID string) (*OAuthResponse, error) {
func RequestOAuthToken(APIlistenPath, redirect_uri, responseType, clientId, secret, orgID, policyID, BaseAPIID, state string) (*OAuthResponse, error) {
// Create a generic access token withour policy
basicSessionState := generateBasicTykSesion(BaseAPIID, "Default", policyID, orgID)
basicSessionState.OauthClientID = clientId
Expand All @@ -177,6 +177,7 @@ func RequestOAuthToken(APIlistenPath, redirect_uri, responseType, clientId, secr
data := "response_type=" + responseType
data += "&client_id=" + clientId
data += "&redirect_uri=" + redirect_uri
date += "&state=" + state
data += "&key_rules=" + url.QueryEscape(string(keyDataJSON))

log.Debug("Request data sent: ", data)
Expand Down