2016-07-25 20:00:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
"crypto/sha256"
|
2020-09-25 15:59:42 +00:00
|
|
|
"crypto/subtle"
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
"encoding/base64"
|
2019-05-27 09:14:15 +00:00
|
|
|
"encoding/json"
|
2016-07-25 20:00:28 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
2016-08-25 23:18:09 +00:00
|
|
|
"sort"
|
2016-07-25 20:00:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-01-13 18:56:09 +00:00
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
2020-02-04 15:07:18 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
jose "gopkg.in/square/go-jose.v2"
|
|
|
|
|
2018-09-03 06:44:44 +00:00
|
|
|
"github.com/dexidp/dex/connector"
|
|
|
|
"github.com/dexidp/dex/server/internal"
|
|
|
|
"github.com/dexidp/dex/storage"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
const (
|
2021-05-27 13:11:12 +00:00
|
|
|
codeChallengeMethodPlain = "plain"
|
|
|
|
codeChallengeMethodS256 = "S256"
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
)
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
func (s *Server) handlePublicKeys(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// TODO(ericchiang): Cache this.
|
|
|
|
keys, err := s.storage.GetKeys()
|
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to get keys: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Internal server error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if keys.SigningKeyPub == nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("No public keys found.")
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Internal server error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jwks := jose.JSONWebKeySet{
|
|
|
|
Keys: make([]jose.JSONWebKey, len(keys.VerificationKeys)+1),
|
|
|
|
}
|
|
|
|
jwks.Keys[0] = *keys.SigningKeyPub
|
|
|
|
for i, verificationKey := range keys.VerificationKeys {
|
|
|
|
jwks.Keys[i+1] = *verificationKey.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.MarshalIndent(jwks, "", " ")
|
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to marshal discovery data: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Internal server error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
maxAge := keys.NextRotation.Sub(s.now())
|
|
|
|
if maxAge < (time.Minute * 2) {
|
|
|
|
maxAge = time.Minute * 2
|
|
|
|
}
|
|
|
|
|
2016-10-26 21:58:18 +00:00
|
|
|
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d, must-revalidate", int(maxAge.Seconds())))
|
2016-07-25 20:00:28 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
type discovery struct {
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
Auth string `json:"authorization_endpoint"`
|
|
|
|
Token string `json:"token_endpoint"`
|
|
|
|
Keys string `json:"jwks_uri"`
|
|
|
|
UserInfo string `json:"userinfo_endpoint"`
|
|
|
|
DeviceEndpoint string `json:"device_authorization_endpoint"`
|
|
|
|
GrantTypes []string `json:"grant_types_supported"`
|
|
|
|
ResponseTypes []string `json:"response_types_supported"`
|
|
|
|
Subjects []string `json:"subject_types_supported"`
|
|
|
|
IDTokenAlgs []string `json:"id_token_signing_alg_values_supported"`
|
|
|
|
CodeChallengeAlgs []string `json:"code_challenge_methods_supported"`
|
|
|
|
Scopes []string `json:"scopes_supported"`
|
|
|
|
AuthMethods []string `json:"token_endpoint_auth_methods_supported"`
|
|
|
|
Claims []string `json:"claims_supported"`
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-14 09:18:48 +00:00
|
|
|
func (s *Server) discoveryHandler() (http.HandlerFunc, error) {
|
2016-07-25 20:00:28 +00:00
|
|
|
d := discovery{
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
Issuer: s.issuerURL.String(),
|
|
|
|
Auth: s.absURL("/auth"),
|
|
|
|
Token: s.absURL("/token"),
|
|
|
|
Keys: s.absURL("/keys"),
|
|
|
|
UserInfo: s.absURL("/userinfo"),
|
|
|
|
DeviceEndpoint: s.absURL("/device/code"),
|
|
|
|
Subjects: []string{"public"},
|
|
|
|
IDTokenAlgs: []string{string(jose.RS256)},
|
2021-05-27 13:11:12 +00:00
|
|
|
CodeChallengeAlgs: []string{codeChallengeMethodS256, codeChallengeMethodPlain},
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
Scopes: []string{"openid", "email", "groups", "profile", "offline_access"},
|
2021-01-20 11:15:30 +00:00
|
|
|
AuthMethods: []string{"client_secret_basic", "client_secret_post"},
|
2016-07-25 20:00:28 +00:00
|
|
|
Claims: []string{
|
2021-01-20 11:15:30 +00:00
|
|
|
"iss", "sub", "aud", "iat", "exp", "email", "email_verified",
|
|
|
|
"locale", "name", "preferred_username", "at_hash",
|
2016-07-25 20:00:28 +00:00
|
|
|
},
|
|
|
|
}
|
2016-08-25 23:18:09 +00:00
|
|
|
|
|
|
|
for responseType := range s.supportedResponseTypes {
|
|
|
|
d.ResponseTypes = append(d.ResponseTypes, responseType)
|
|
|
|
}
|
|
|
|
sort.Strings(d.ResponseTypes)
|
|
|
|
|
2021-09-03 09:48:37 +00:00
|
|
|
d.GrantTypes = s.supportedGrantTypes
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
data, err := json.MarshalIndent(d, "", " ")
|
|
|
|
if err != nil {
|
2016-08-25 23:18:09 +00:00
|
|
|
return nil, fmt.Errorf("failed to marshal discovery data: %v", err)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-08-25 23:18:09 +00:00
|
|
|
|
2017-01-14 09:18:48 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-08-25 23:18:09 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
|
|
|
w.Write(data)
|
2017-01-14 09:18:48 +00:00
|
|
|
}), nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleAuthorization handles the OAuth2 auth endpoint.
|
|
|
|
func (s *Server) handleAuthorization(w http.ResponseWriter, r *http.Request) {
|
2020-11-13 18:19:06 +00:00
|
|
|
// Extract the arguments
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
s.logger.Errorf("Failed to parse arguments: %v", err)
|
2017-01-09 18:46:16 +00:00
|
|
|
|
2021-05-21 10:03:22 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, err.Error())
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2017-01-09 18:46:16 +00:00
|
|
|
|
2020-11-13 18:19:06 +00:00
|
|
|
connectorID := r.Form.Get("connector_id")
|
2017-02-02 18:29:57 +00:00
|
|
|
|
2019-07-24 10:45:50 +00:00
|
|
|
connectors, err := s.storage.ListConnectors()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Failed to get list of connectors: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Failed to retrieve connector list.")
|
2017-04-17 22:41:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-21 10:03:22 +00:00
|
|
|
// We don't need connector_id any more
|
|
|
|
r.Form.Del("connector_id")
|
|
|
|
|
2020-11-13 18:19:06 +00:00
|
|
|
// Construct a URL with all of the arguments in its query
|
|
|
|
connURL := url.URL{
|
|
|
|
RawQuery: r.Form.Encode(),
|
|
|
|
}
|
|
|
|
|
2019-07-22 15:47:11 +00:00
|
|
|
// Redirect if a client chooses a specific connector_id
|
2020-11-13 18:19:06 +00:00
|
|
|
if connectorID != "" {
|
2019-07-22 15:47:11 +00:00
|
|
|
for _, c := range connectors {
|
2020-11-13 18:19:06 +00:00
|
|
|
if c.ID == connectorID {
|
|
|
|
connURL.Path = s.absPath("/auth", c.ID)
|
|
|
|
http.Redirect(w, r, connURL.String(), http.StatusFound)
|
2019-07-22 15:47:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 19:02:09 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Connector ID does not match a valid Connector")
|
2019-07-22 15:47:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-06 17:18:46 +00:00
|
|
|
if len(connectors) == 1 && !s.alwaysShowLogin {
|
2021-05-21 10:03:22 +00:00
|
|
|
connURL.Path = s.absPath("/auth", connectors[0].ID)
|
|
|
|
http.Redirect(w, r, connURL.String(), http.StatusFound)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
connectorInfos := make([]connectorInfo, len(connectors))
|
2019-10-29 23:33:52 +00:00
|
|
|
for index, conn := range connectors {
|
2020-11-13 18:19:06 +00:00
|
|
|
connURL.Path = s.absPath("/auth", conn.ID)
|
2019-10-29 23:33:52 +00:00
|
|
|
connectorInfos[index] = connectorInfo{
|
2017-04-17 22:41:41 +00:00
|
|
|
ID: conn.ID,
|
|
|
|
Name: conn.Name,
|
2019-10-29 23:33:52 +00:00
|
|
|
Type: conn.Type,
|
2020-11-13 18:19:06 +00:00
|
|
|
URL: connURL.String(),
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:02:29 +00:00
|
|
|
if err := s.templates.login(r, w, connectorInfos); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleConnectorLogin(w http.ResponseWriter, r *http.Request) {
|
2020-11-13 18:19:06 +00:00
|
|
|
authReq, err := s.parseAuthorizationRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Failed to parse authorization request: %v", err)
|
2020-11-11 19:02:09 +00:00
|
|
|
|
|
|
|
switch authErr := err.(type) {
|
|
|
|
case *redirectedAuthErr:
|
|
|
|
authErr.Handler().ServeHTTP(w, r)
|
|
|
|
case *displayedAuthErr:
|
|
|
|
s.renderError(r, w, authErr.Status, err.Error())
|
|
|
|
default:
|
|
|
|
panic("unsupported error type")
|
2020-11-13 18:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
connID := mux.Vars(r)["connector"]
|
2017-04-17 22:41:41 +00:00
|
|
|
conn, err := s.getConnector(connID)
|
|
|
|
if err != nil {
|
2020-05-07 13:15:43 +00:00
|
|
|
s.logger.Errorf("Failed to get connector: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-13 18:19:06 +00:00
|
|
|
// Set the connector being used for the login.
|
|
|
|
if authReq.ConnectorID != "" && authReq.ConnectorID != connID {
|
|
|
|
s.logger.Errorf("Mismatched connector ID in auth request: %s vs %s",
|
|
|
|
authReq.ConnectorID, connID)
|
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Bad connector ID")
|
2016-11-18 21:40:41 +00:00
|
|
|
return
|
|
|
|
}
|
2017-12-08 10:49:47 +00:00
|
|
|
|
2020-11-13 18:19:06 +00:00
|
|
|
authReq.ConnectorID = connID
|
|
|
|
|
|
|
|
// Actually create the auth request
|
|
|
|
authReq.Expiry = s.now().Add(s.authRequestsValidFor)
|
|
|
|
if err := s.storage.CreateAuthRequest(*authReq); err != nil {
|
|
|
|
s.logger.Errorf("Failed to create authorization request: %v", err)
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Failed to connect to the database.")
|
|
|
|
return
|
2017-12-08 10:49:47 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 21:40:41 +00:00
|
|
|
scopes := parseScopes(authReq.Scopes)
|
2020-11-16 11:29:14 +00:00
|
|
|
|
|
|
|
// Work out where the "Select another login method" link should go.
|
|
|
|
backLink := ""
|
|
|
|
if len(s.connectors) > 1 {
|
|
|
|
backLinkURL := url.URL{
|
|
|
|
Path: s.absPath("/auth"),
|
|
|
|
RawQuery: r.Form.Encode(),
|
|
|
|
}
|
|
|
|
backLink = backLinkURL.String()
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
|
|
|
|
switch r.Method {
|
2018-12-27 08:26:39 +00:00
|
|
|
case http.MethodGet:
|
2016-07-25 20:00:28 +00:00
|
|
|
switch conn := conn.Connector.(type) {
|
|
|
|
case connector.CallbackConnector:
|
2016-10-27 17:20:30 +00:00
|
|
|
// Use the auth request ID as the "state" token.
|
|
|
|
//
|
|
|
|
// TODO(ericchiang): Is this appropriate or should we also be using a nonce?
|
2020-11-13 18:19:06 +00:00
|
|
|
callbackURL, err := conn.LoginURL(scopes, s.absURL("/callback"), authReq.ID)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Connector %q returned error when creating callback: %v", connID, err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Login error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, callbackURL, http.StatusFound)
|
|
|
|
case connector.PasswordConnector:
|
2021-05-21 10:03:22 +00:00
|
|
|
loginURL := url.URL{
|
|
|
|
Path: s.absPath("/auth", connID, "login"),
|
2016-12-12 22:54:01 +00:00
|
|
|
}
|
2021-05-21 10:03:22 +00:00
|
|
|
q := loginURL.Query()
|
|
|
|
q.Set("state", authReq.ID)
|
|
|
|
q.Set("back", backLink)
|
|
|
|
loginURL.RawQuery = q.Encode()
|
|
|
|
|
|
|
|
http.Redirect(w, r, loginURL.String(), http.StatusFound)
|
2016-12-21 01:24:32 +00:00
|
|
|
case connector.SAMLConnector:
|
2020-11-13 18:19:06 +00:00
|
|
|
action, value, err := conn.POSTData(scopes, authReq.ID)
|
2016-12-21 01:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Creating SAML data: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Connector Login Error")
|
2016-12-21 01:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ericchiang): Don't inline this.
|
|
|
|
fmt.Fprintf(w, `<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
|
|
|
<title>SAML login</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form method="post" action="%s" >
|
|
|
|
<input type="hidden" name="SAMLRequest" value="%s" />
|
|
|
|
<input type="hidden" name="RelayState" value="%s" />
|
|
|
|
</form>
|
|
|
|
<script>
|
|
|
|
document.forms[0].submit();
|
|
|
|
</script>
|
|
|
|
</body>
|
2020-11-13 18:19:06 +00:00
|
|
|
</html>`, action, value, authReq.ID)
|
2016-07-25 20:00:28 +00:00
|
|
|
default:
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2021-05-21 10:03:22 +00:00
|
|
|
default:
|
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Unsupported request method.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handlePasswordLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
authID := r.URL.Query().Get("state")
|
|
|
|
if authID == "" {
|
|
|
|
s.renderError(r, w, http.StatusBadRequest, "User session error.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
backLink := r.URL.Query().Get("back")
|
|
|
|
|
|
|
|
authReq, err := s.storage.GetAuthRequest(authID)
|
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrNotFound {
|
|
|
|
s.logger.Errorf("Invalid 'state' parameter provided: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-21 10:03:22 +00:00
|
|
|
s.logger.Errorf("Failed to get auth request: %v", err)
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Database error.")
|
|
|
|
return
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2021-05-21 10:03:22 +00:00
|
|
|
if connID := mux.Vars(r)["connector"]; connID != "" && connID != authReq.ConnectorID {
|
|
|
|
s.logger.Errorf("Connector mismatch: authentication started with id %q, but password login for id %q was triggered", authReq.ConnectorID, connID)
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := s.getConnector(authReq.ConnectorID)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Failed to get connector with id %q : %v", authReq.ConnectorID, err)
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pwConn, ok := conn.Connector.(connector.PasswordConnector)
|
|
|
|
if !ok {
|
|
|
|
s.logger.Errorf("Expected password connector in handlePasswordLogin(), but got %v", pwConn)
|
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodGet:
|
|
|
|
if err := s.templates.password(r, w, r.URL.String(), "", usernamePrompt(pwConn), false, backLink); err != nil {
|
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
|
|
|
case http.MethodPost:
|
2016-08-25 20:10:19 +00:00
|
|
|
username := r.FormValue("login")
|
2016-07-25 20:00:28 +00:00
|
|
|
password := r.FormValue("password")
|
2021-05-21 10:03:22 +00:00
|
|
|
scopes := parseScopes(authReq.Scopes)
|
2016-07-25 20:00:28 +00:00
|
|
|
|
2021-05-21 10:03:22 +00:00
|
|
|
identity, ok, err := pwConn.Login(r.Context(), scopes, username, password)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to login user: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, fmt.Sprintf("Login error: %v", err))
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
2021-05-21 10:03:22 +00:00
|
|
|
if err := s.templates.password(r, w, r.URL.String(), username, usernamePrompt(pwConn), true, backLink); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-10-27 17:08:08 +00:00
|
|
|
redirectURL, err := s.finalizeLogin(identity, authReq, conn.Connector)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to finalize login: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Login error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:14:24 +00:00
|
|
|
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
|
2016-07-25 20:00:28 +00:00
|
|
|
default:
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Unsupported request method.")
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleConnectorCallback(w http.ResponseWriter, r *http.Request) {
|
2016-12-21 01:24:32 +00:00
|
|
|
var authID string
|
|
|
|
switch r.Method {
|
2018-12-27 08:26:39 +00:00
|
|
|
case http.MethodGet: // OAuth2 callback
|
2016-12-21 01:24:32 +00:00
|
|
|
if authID = r.URL.Query().Get("state"); authID == "" {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "User session error.")
|
2016-12-21 01:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
2018-12-27 08:26:39 +00:00
|
|
|
case http.MethodPost: // SAML POST binding
|
2016-12-21 01:24:32 +00:00
|
|
|
if authID = r.PostFormValue("RelayState"); authID == "" {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "User session error.")
|
2016-12-21 01:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Method not supported")
|
2016-10-27 17:08:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-21 01:24:32 +00:00
|
|
|
authReq, err := s.storage.GetAuthRequest(authID)
|
2016-10-27 17:08:08 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == storage.ErrNotFound {
|
2016-12-14 22:17:59 +00:00
|
|
|
s.logger.Errorf("Invalid 'state' parameter provided: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Requested resource does not exist.")
|
2016-10-27 17:08:08 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to get auth request: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Database error.")
|
2016-10-27 17:08:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:54:54 +00:00
|
|
|
if connID := mux.Vars(r)["connector"]; connID != "" && connID != authReq.ConnectorID {
|
|
|
|
s.logger.Errorf("Connector mismatch: authentication started with id %q, but callback for id %q was triggered", authReq.ConnectorID, connID)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
2017-10-21 14:54:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-17 22:41:41 +00:00
|
|
|
conn, err := s.getConnector(authReq.ConnectorID)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Failed to get connector with id %q : %v", authReq.ConnectorID, err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-12-21 01:24:32 +00:00
|
|
|
|
|
|
|
var identity connector.Identity
|
|
|
|
switch conn := conn.Connector.(type) {
|
|
|
|
case connector.CallbackConnector:
|
2018-12-27 08:26:39 +00:00
|
|
|
if r.Method != http.MethodGet {
|
2016-12-21 01:24:32 +00:00
|
|
|
s.logger.Errorf("SAML request mapped to OAuth2 connector")
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Invalid request")
|
2016-12-21 01:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
identity, err = conn.HandleCallback(parseScopes(authReq.Scopes), r)
|
|
|
|
case connector.SAMLConnector:
|
2018-12-27 08:26:39 +00:00
|
|
|
if r.Method != http.MethodPost {
|
2016-12-21 01:24:32 +00:00
|
|
|
s.logger.Errorf("OAuth2 request mapped to SAML connector")
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "Invalid request")
|
2016-12-21 01:24:32 +00:00
|
|
|
return
|
|
|
|
}
|
2017-03-21 20:16:42 +00:00
|
|
|
identity, err = conn.HandlePOST(parseScopes(authReq.Scopes), r.PostFormValue("SAMLResponse"), authReq.ID)
|
2016-12-21 01:24:32 +00:00
|
|
|
default:
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Requested resource does not exist.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to authenticate: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, fmt.Sprintf("Failed to authenticate: %v", err))
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:14:24 +00:00
|
|
|
|
2016-10-27 17:08:08 +00:00
|
|
|
redirectURL, err := s.finalizeLogin(identity, authReq, conn.Connector)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to finalize login: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Login error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:14:24 +00:00
|
|
|
|
|
|
|
http.Redirect(w, r, redirectURL, http.StatusSeeOther)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 17:17:30 +00:00
|
|
|
// finalizeLogin associates the user's identity with the current AuthRequest, then returns
|
|
|
|
// the approval page's path.
|
2016-10-27 17:08:08 +00:00
|
|
|
func (s *Server) finalizeLogin(identity connector.Identity, authReq storage.AuthRequest, conn connector.Connector) (string, error) {
|
2016-08-03 04:57:36 +00:00
|
|
|
claims := storage.Claims{
|
2019-10-10 14:43:41 +00:00
|
|
|
UserID: identity.UserID,
|
|
|
|
Username: identity.Username,
|
|
|
|
PreferredUsername: identity.PreferredUsername,
|
|
|
|
Email: identity.Email,
|
|
|
|
EmailVerified: identity.EmailVerified,
|
|
|
|
Groups: identity.Groups,
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-08-03 04:14:24 +00:00
|
|
|
|
|
|
|
updater := func(a storage.AuthRequest) (storage.AuthRequest, error) {
|
2016-09-14 23:38:12 +00:00
|
|
|
a.LoggedIn = true
|
|
|
|
a.Claims = claims
|
2019-04-18 12:52:05 +00:00
|
|
|
a.ConnectorData = identity.ConnectorData
|
2016-08-03 04:14:24 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
2016-10-27 17:08:08 +00:00
|
|
|
if err := s.storage.UpdateAuthRequest(authReq.ID, updater); err != nil {
|
2016-08-03 04:14:24 +00:00
|
|
|
return "", fmt.Errorf("failed to update auth request: %v", err)
|
|
|
|
}
|
2017-08-11 17:17:30 +00:00
|
|
|
|
|
|
|
email := claims.Email
|
|
|
|
if !claims.EmailVerified {
|
2020-10-17 21:54:27 +00:00
|
|
|
email += " (unverified)"
|
2017-08-11 17:17:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 14:43:41 +00:00
|
|
|
s.logger.Infof("login successful: connector %q, username=%q, preferred_username=%q, email=%q, groups=%q",
|
2019-10-30 12:13:33 +00:00
|
|
|
authReq.ConnectorID, claims.Username, claims.PreferredUsername, email, claims.Groups)
|
2017-08-11 17:17:30 +00:00
|
|
|
|
2019-09-25 20:27:31 +00:00
|
|
|
returnURL := path.Join(s.issuerURL.Path, "/approval") + "?req=" + authReq.ID
|
|
|
|
_, ok := conn.(connector.RefreshConnector)
|
|
|
|
if !ok {
|
|
|
|
return returnURL, nil
|
|
|
|
}
|
2018-01-28 22:37:07 +00:00
|
|
|
|
2019-09-25 20:27:31 +00:00
|
|
|
// Try to retrieve an existing OfflineSession object for the corresponding user.
|
2020-10-17 21:54:27 +00:00
|
|
|
session, err := s.storage.GetOfflineSessions(identity.UserID, authReq.ConnectorID)
|
|
|
|
if err != nil {
|
2019-09-25 20:27:31 +00:00
|
|
|
if err != storage.ErrNotFound {
|
|
|
|
s.logger.Errorf("failed to get offline session: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
offlineSessions := storage.OfflineSessions{
|
|
|
|
UserID: identity.UserID,
|
|
|
|
ConnID: authReq.ConnectorID,
|
|
|
|
Refresh: make(map[string]*storage.RefreshTokenRef),
|
|
|
|
ConnectorData: identity.ConnectorData,
|
|
|
|
}
|
2018-01-28 22:37:07 +00:00
|
|
|
|
2019-09-25 20:27:31 +00:00
|
|
|
// Create a new OfflineSession object for the user and add a reference object for
|
|
|
|
// the newly received refreshtoken.
|
|
|
|
if err := s.storage.CreateOfflineSessions(offlineSessions); err != nil {
|
|
|
|
s.logger.Errorf("failed to create offline session: %v", err)
|
|
|
|
return "", err
|
|
|
|
}
|
2020-10-17 21:54:27 +00:00
|
|
|
|
|
|
|
return returnURL, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update existing OfflineSession obj with new RefreshTokenRef.
|
|
|
|
if err := s.storage.UpdateOfflineSessions(session.UserID, session.ConnID, func(old storage.OfflineSessions) (storage.OfflineSessions, error) {
|
|
|
|
if len(identity.ConnectorData) > 0 {
|
|
|
|
old.ConnectorData = identity.ConnectorData
|
2018-01-28 22:37:07 +00:00
|
|
|
}
|
2020-10-17 21:54:27 +00:00
|
|
|
return old, nil
|
|
|
|
}); err != nil {
|
|
|
|
s.logger.Errorf("failed to update offline session: %v", err)
|
|
|
|
return "", err
|
2018-01-28 22:37:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:27:31 +00:00
|
|
|
return returnURL, nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleApproval(w http.ResponseWriter, r *http.Request) {
|
2016-10-27 17:20:30 +00:00
|
|
|
authReq, err := s.storage.GetAuthRequest(r.FormValue("req"))
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to get auth request: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Database error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-09-14 23:38:12 +00:00
|
|
|
if !authReq.LoggedIn {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Auth request does not have an identity for approval")
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Login process not yet finalized.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
2018-12-27 08:26:39 +00:00
|
|
|
case http.MethodGet:
|
2016-07-25 20:00:28 +00:00
|
|
|
if s.skipApproval {
|
2016-08-03 04:57:36 +00:00
|
|
|
s.sendCodeResponse(w, r, authReq)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
client, err := s.storage.GetClient(authReq.ClientID)
|
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to get client %q: %v", authReq.ClientID, err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Failed to retrieve client.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-17 21:02:29 +00:00
|
|
|
if err := s.templates.approval(r, w, authReq.ID, authReq.Claims.Username, client.Name, authReq.Scopes); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
2018-12-27 08:26:39 +00:00
|
|
|
case http.MethodPost:
|
2016-07-25 20:00:28 +00:00
|
|
|
if r.FormValue("approval") != "approve" {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Approval rejected.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2016-08-03 04:57:36 +00:00
|
|
|
s.sendCodeResponse(w, r, authReq)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:57:36 +00:00
|
|
|
func (s *Server) sendCodeResponse(w http.ResponseWriter, r *http.Request, authReq storage.AuthRequest) {
|
2016-10-13 01:50:48 +00:00
|
|
|
if s.now().After(authReq.Expiry) {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "User session has expired.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.storage.DeleteAuthRequest(authReq.ID); err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to delete authorization request: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Internal server error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
} else {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusBadRequest, "User session error.")
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
u, err := url.Parse(authReq.RedirectURI)
|
|
|
|
if err != nil {
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Invalid redirect URI.")
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
2017-01-09 18:46:16 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// Was the initial request using the implicit or hybrid flow instead of
|
|
|
|
// the "normal" code flow?
|
|
|
|
implicitOrHybrid = false
|
|
|
|
|
|
|
|
// Only present in hybrid or code flow. code.ID == "" if this is not set.
|
|
|
|
code storage.AuthCode
|
|
|
|
|
|
|
|
// ID token returned immediately if the response_type includes "id_token".
|
|
|
|
// Only valid for implicit and hybrid flows.
|
|
|
|
idToken string
|
|
|
|
idTokenExpiry time.Time
|
2017-01-11 01:51:12 +00:00
|
|
|
|
2019-05-27 07:41:00 +00:00
|
|
|
// Access token
|
2019-05-27 09:14:15 +00:00
|
|
|
accessToken string
|
2017-01-09 18:46:16 +00:00
|
|
|
)
|
2016-08-20 00:24:49 +00:00
|
|
|
|
|
|
|
for _, responseType := range authReq.ResponseTypes {
|
|
|
|
switch responseType {
|
|
|
|
case responseTypeCode:
|
2017-01-09 18:46:16 +00:00
|
|
|
code = storage.AuthCode{
|
2019-04-18 12:52:05 +00:00
|
|
|
ID: storage.NewID(),
|
|
|
|
ClientID: authReq.ClientID,
|
|
|
|
ConnectorID: authReq.ConnectorID,
|
|
|
|
Nonce: authReq.Nonce,
|
|
|
|
Scopes: authReq.Scopes,
|
|
|
|
Claims: authReq.Claims,
|
|
|
|
Expiry: s.now().Add(time.Minute * 30),
|
|
|
|
RedirectURI: authReq.RedirectURI,
|
|
|
|
ConnectorData: authReq.ConnectorData,
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
PKCE: authReq.PKCE,
|
2016-08-20 00:24:49 +00:00
|
|
|
}
|
|
|
|
if err := s.storage.CreateAuthCode(code); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Failed to create auth code: %v", err)
|
2019-09-27 13:56:32 +00:00
|
|
|
s.renderError(r, w, http.StatusInternalServerError, "Internal server error.")
|
2016-08-20 00:24:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:46:16 +00:00
|
|
|
// Implicit and hybrid flows that try to use the OOB redirect URI are
|
|
|
|
// rejected earlier. If we got here we're using the code flow.
|
2016-08-20 00:24:49 +00:00
|
|
|
if authReq.RedirectURI == redirectURIOOB {
|
2020-10-17 21:02:29 +00:00
|
|
|
if err := s.templates.oob(r, w, code.ID); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
2016-08-20 00:24:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case responseTypeToken:
|
2017-01-09 18:46:16 +00:00
|
|
|
implicitOrHybrid = true
|
|
|
|
case responseTypeIDToken:
|
|
|
|
implicitOrHybrid = true
|
|
|
|
var err error
|
2019-05-27 07:41:00 +00:00
|
|
|
|
2019-06-27 23:12:18 +00:00
|
|
|
accessToken, err = s.newAccessToken(authReq.ClientID, authReq.Claims, authReq.Scopes, authReq.Nonce, authReq.ConnectorID)
|
2019-05-27 07:41:00 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("failed to create new access token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:01:40 +00:00
|
|
|
idToken, idTokenExpiry, err = s.newIDToken(authReq.ClientID, authReq.Claims, authReq.Scopes, authReq.Nonce, accessToken, code.ID, authReq.ConnectorID)
|
2016-08-20 00:24:49 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to create ID token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2016-08-20 00:24:49 +00:00
|
|
|
return
|
|
|
|
}
|
2017-01-09 18:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if implicitOrHybrid {
|
|
|
|
v := url.Values{}
|
2017-01-11 01:51:12 +00:00
|
|
|
v.Set("access_token", accessToken)
|
2017-01-09 18:46:16 +00:00
|
|
|
v.Set("token_type", "bearer")
|
|
|
|
v.Set("state", authReq.State)
|
|
|
|
if idToken != "" {
|
2016-08-20 00:24:49 +00:00
|
|
|
v.Set("id_token", idToken)
|
2017-01-09 18:46:16 +00:00
|
|
|
// The hybrid flow with only "code token" or "code id_token" doesn't return an
|
|
|
|
// "expires_in" value. If "code" wasn't provided, indicating the implicit flow,
|
|
|
|
// don't add it.
|
|
|
|
//
|
|
|
|
// https://openid.net/specs/openid-connect-core-1_0.html#HybridAuthResponse
|
|
|
|
if code.ID == "" {
|
|
|
|
v.Set("expires_in", strconv.Itoa(int(idTokenExpiry.Sub(s.now()).Seconds())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if code.ID != "" {
|
|
|
|
v.Set("code", code.ID)
|
2016-08-20 00:24:49 +00:00
|
|
|
}
|
2017-01-09 18:46:16 +00:00
|
|
|
|
|
|
|
// Implicit and hybrid flows return their values as part of the fragment.
|
|
|
|
//
|
|
|
|
// HTTP/1.1 303 See Other
|
|
|
|
// Location: https://client.example.org/cb#
|
|
|
|
// access_token=SlAV32hkKG
|
|
|
|
// &token_type=bearer
|
|
|
|
// &id_token=eyJ0 ... NiJ9.eyJ1c ... I6IjIifX0.DeWt4Qu ... ZXso
|
|
|
|
// &expires_in=3600
|
|
|
|
// &state=af0ifjsldkj
|
|
|
|
//
|
|
|
|
u.Fragment = v.Encode()
|
|
|
|
} else {
|
|
|
|
// The code flow add values to the URL query.
|
|
|
|
//
|
|
|
|
// HTTP/1.1 303 See Other
|
|
|
|
// Location: https://client.example.org/cb?
|
|
|
|
// code=SplxlOBeZQQYbYS6WxSbIA
|
|
|
|
// &state=af0ifjsldkj
|
|
|
|
//
|
|
|
|
q := u.Query()
|
|
|
|
q.Set("code", code.ID)
|
|
|
|
q.Set("state", authReq.State)
|
|
|
|
u.RawQuery = q.Encode()
|
2016-08-20 00:24:49 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
http.Redirect(w, r, u.String(), http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:41:19 +00:00
|
|
|
func (s *Server) withClientFromStorage(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request, storage.Client)) {
|
2016-07-25 20:00:28 +00:00
|
|
|
clientID, clientSecret, ok := r.BasicAuth()
|
|
|
|
if ok {
|
|
|
|
var err error
|
|
|
|
if clientID, err = url.QueryUnescape(clientID); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "client_id improperly encoded", http.StatusBadRequest)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if clientSecret, err = url.QueryUnescape(clientSecret); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "client_secret improperly encoded", http.StatusBadRequest)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
clientID = r.PostFormValue("client_id")
|
|
|
|
clientSecret = r.PostFormValue("client_secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := s.storage.GetClient(clientID)
|
|
|
|
if err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to get client: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2016-07-25 20:00:28 +00:00
|
|
|
} else {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidClient, "Invalid client credentials.", http.StatusUnauthorized)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-04-17 20:27:02 +00:00
|
|
|
|
2021-05-17 14:16:50 +00:00
|
|
|
if subtle.ConstantTimeCompare([]byte(client.Secret), []byte(clientSecret)) != 1 {
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
if clientSecret == "" {
|
|
|
|
s.logger.Infof("missing client_secret on token request for client: %s", client.ID)
|
|
|
|
} else {
|
|
|
|
s.logger.Infof("invalid client_secret on token request for client: %s", client.ID)
|
|
|
|
}
|
2016-12-12 22:54:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidClient, "Invalid client credentials.", http.StatusUnauthorized)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:41:19 +00:00
|
|
|
handler(w, r, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) handleToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "method not allowed", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("Could not parse request body: %v", err)
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
grantType := r.PostFormValue("grant_type")
|
|
|
|
switch grantType {
|
2021-02-19 15:41:19 +00:00
|
|
|
case grantTypeDeviceCode:
|
|
|
|
s.handleDeviceToken(w, r)
|
2016-08-24 18:14:38 +00:00
|
|
|
case grantTypeAuthorizationCode:
|
2021-02-19 15:41:19 +00:00
|
|
|
s.withClientFromStorage(w, r, s.handleAuthCode)
|
2016-08-24 18:14:38 +00:00
|
|
|
case grantTypeRefreshToken:
|
2021-02-19 15:41:19 +00:00
|
|
|
s.withClientFromStorage(w, r, s.handleRefreshToken)
|
2018-01-03 03:15:01 +00:00
|
|
|
case grantTypePassword:
|
2021-02-19 15:41:19 +00:00
|
|
|
s.withClientFromStorage(w, r, s.handlePasswordGrant)
|
2016-07-25 20:00:28 +00:00
|
|
|
default:
|
2020-11-11 19:02:09 +00:00
|
|
|
s.tokenErrHelper(w, errUnsupportedGrantType, "", http.StatusBadRequest)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
func (s *Server) calculateCodeChallenge(codeVerifier, codeChallengeMethod string) (string, error) {
|
|
|
|
switch codeChallengeMethod {
|
2021-05-27 13:11:12 +00:00
|
|
|
case codeChallengeMethodPlain:
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
return codeVerifier, nil
|
2021-05-27 13:11:12 +00:00
|
|
|
case codeChallengeMethodS256:
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
shaSum := sha256.Sum256([]byte(codeVerifier))
|
|
|
|
return base64.RawURLEncoding.EncodeToString(shaSum[:]), nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unknown challenge method (%v)", codeChallengeMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
// handle an access token request https://tools.ietf.org/html/rfc6749#section-4.1.3
|
|
|
|
func (s *Server) handleAuthCode(w http.ResponseWriter, r *http.Request, client storage.Client) {
|
|
|
|
code := r.PostFormValue("code")
|
|
|
|
redirectURI := r.PostFormValue("redirect_uri")
|
|
|
|
|
2021-01-25 14:50:36 +00:00
|
|
|
if code == "" {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, `Required param: code.`, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
authCode, err := s.storage.GetAuthCode(code)
|
|
|
|
if err != nil || s.now().After(authCode.Expiry) || authCode.ClientID != client.ID {
|
|
|
|
if err != storage.ErrNotFound {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to get auth code: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2016-07-25 20:00:28 +00:00
|
|
|
} else {
|
2021-01-20 21:31:38 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidGrant, "Invalid or expired code parameter.", http.StatusBadRequest)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
// RFC 7636 (PKCE)
|
|
|
|
codeChallengeFromStorage := authCode.PKCE.CodeChallenge
|
|
|
|
providedCodeVerifier := r.PostFormValue("code_verifier")
|
|
|
|
|
2020-10-26 19:20:33 +00:00
|
|
|
switch {
|
|
|
|
case providedCodeVerifier != "" && codeChallengeFromStorage != "":
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
calculatedCodeChallenge, err := s.calculateCodeChallenge(providedCodeVerifier, authCode.PKCE.CodeChallengeMethod)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error(err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if codeChallengeFromStorage != calculatedCodeChallenge {
|
|
|
|
s.tokenErrHelper(w, errInvalidGrant, "Invalid code_verifier.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-10-26 19:20:33 +00:00
|
|
|
case providedCodeVerifier != "":
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
// Received no code_challenge on /auth, but a code_verifier on /token
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "No PKCE flow started. Cannot check code_verifier.", http.StatusBadRequest)
|
|
|
|
return
|
2020-10-26 19:20:33 +00:00
|
|
|
case codeChallengeFromStorage != "":
|
PKCE implementation (#1784)
* Basic implementation of PKCE
Signed-off-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
* @mfmarche on 24 Feb: when code_verifier is set, don't check client_secret
In PKCE flow, no client_secret is used, so the check for a valid client_secret
would always fail.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* @deric on 16 Jun: return invalid_grant when wrong code_verifier
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enforce PKCE flow on /token when PKCE flow was started on /auth
Also dissallow PKCE on /token, when PKCE flow was not started on /auth
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* fixed error messages when mixed PKCE/no PKCE flow.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* server_test.go: Added PKCE error cases on /token endpoint
* Added test for invalid_grant, when wrong code_verifier is sent
* Added test for mixed PKCE / no PKCE auth flows.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* cleanup: extracted method checkErrorResponse and type TestDefinition
* fixed connector being overwritten
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* /token endpoint: skip client_secret verification only for grand type authorization_code with PKCE extension
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow "Authorization" header in CORS handlers
* Adds "Authorization" to the default CORS headers{"Accept", "Accept-Language", "Content-Language", "Origin"}
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Add "code_challenge_methods_supported" to discovery endpoint
discovery endpoint /dex/.well-known/openid-configuration
now has the following entry:
"code_challenge_methods_supported": [
"S256",
"plain"
]
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Updated tests (mixed-up comments), added a PKCE test
* @asoorm added test that checks if downgrade to "plain" on /token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* remove redefinition of providedCodeVerifier, fixed spelling (#6)
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Signed-off-by: Bernd Eckstein <HEllRZA@users.noreply.github.com>
* Rename struct CodeChallenge to PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* PKCE: Check clientSecret when available
In authorization_code flow with PKCE, allow empty client_secret on /auth and /token endpoints. But check the client_secret when it is given.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Enable PKCE with public: true
dex configuration public on staticClients now enables the following behavior in PKCE:
- Public: false, PKCE will always check client_secret. This means PKCE in it's natural form is disabled.
- Public: true, PKCE is enabled. It will only check client_secret if the client has sent one. But it allows the code flow if the client didn't sent one.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Redirect error on unsupported code_challenge_method
- Check for unsupported code_challenge_method after redirect uri is validated, and use newErr() to return the error.
- Add PKCE tests to oauth2_test.go
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Reverted go.mod and go.sum to the state of master
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Don't omit client secret check for PKCE
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Allow public clients (e.g. with PKCE) to have redirect URIs configured
Signed-off-by: Martin Heide <martin.heide@faro.com>
* Remove "Authorization" as Accepted Headers on CORS, small fixes
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Revert "Allow public clients (e.g. with PKCE) to have redirect URIs configured"
This reverts commit b6e297b78537dc44cd3e1374f0b4d34bf89404ac.
Signed-off-by: Martin Heide <martin.heide@faro.com>
* PKCE on client_secret client error message
* When connecting to the token endpoint with PKCE without client_secret, but the client is configured with a client_secret, generate a special error message.
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* Output info message when PKCE without client_secret used on confidential client
* removes the special error message
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
* General missing/invalid client_secret message on token endpoint
Signed-off-by: Bernd Eckstein <Bernd.Eckstein@faro.com>
Co-authored-by: Tadeusz Magura-Witkowski <tadeuszmw@gmail.com>
Co-authored-by: Martin Heide <martin.heide@faro.com>
Co-authored-by: M. Heide <66078329+heidemn-faro@users.noreply.github.com>
2020-10-26 10:33:40 +00:00
|
|
|
// Received PKCE request on /auth, but no code_verifier on /token
|
|
|
|
s.tokenErrHelper(w, errInvalidGrant, "Expecting parameter code_verifier in PKCE flow.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
if authCode.RedirectURI != redirectURI {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "redirect_uri did not match URI from initial request.", http.StatusBadRequest)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:14:30 +00:00
|
|
|
tokenResponse, err := s.exchangeAuthCode(w, authCode, client)
|
|
|
|
if err != nil {
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.writeAccessToken(w, tokenResponse)
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:01:51 +00:00
|
|
|
func (s *Server) exchangeAuthCode(w http.ResponseWriter, authCode storage.AuthCode, client storage.Client) (*accessTokenResponse, error) {
|
2019-05-27 07:41:00 +00:00
|
|
|
accessToken, err := s.newAccessToken(client.ID, authCode.Claims, authCode.Scopes, authCode.Nonce, authCode.ConnectorID)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("failed to create new access token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2019-05-27 07:41:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 12:01:40 +00:00
|
|
|
idToken, expiry, err := s.newIDToken(client.ID, authCode.Claims, authCode.Scopes, authCode.Nonce, accessToken, authCode.ID, authCode.ConnectorID)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to create ID token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:14:30 +00:00
|
|
|
if err := s.storage.DeleteAuthCode(authCode.ID); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to delete auth code: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
reqRefresh := func() bool {
|
2017-03-23 21:06:30 +00:00
|
|
|
// Ensure the connector supports refresh tokens.
|
|
|
|
//
|
2017-04-11 00:31:07 +00:00
|
|
|
// Connectors like `saml` do not implement RefreshConnector.
|
2017-04-17 22:41:41 +00:00
|
|
|
conn, err := s.getConnector(authCode.ConnectorID)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("connector with ID %q not found: %v", authCode.ConnectorID, err)
|
2017-03-23 21:06:30 +00:00
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return false
|
|
|
|
}
|
2017-04-17 22:41:41 +00:00
|
|
|
|
|
|
|
_, ok := conn.Connector.(connector.RefreshConnector)
|
2017-03-23 21:06:30 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
for _, scope := range authCode.Scopes {
|
|
|
|
if scope == scopeOfflineAccess {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
var refreshToken string
|
|
|
|
if reqRefresh {
|
2016-08-03 04:57:36 +00:00
|
|
|
refresh := storage.RefreshToken{
|
2019-04-18 12:52:05 +00:00
|
|
|
ID: storage.NewID(),
|
|
|
|
Token: storage.NewID(),
|
|
|
|
ClientID: authCode.ClientID,
|
|
|
|
ConnectorID: authCode.ConnectorID,
|
|
|
|
Scopes: authCode.Scopes,
|
|
|
|
Claims: authCode.Claims,
|
|
|
|
Nonce: authCode.Nonce,
|
|
|
|
ConnectorData: authCode.ConnectorData,
|
|
|
|
CreatedAt: s.now(),
|
|
|
|
LastUsed: s.now(),
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-12-23 00:41:30 +00:00
|
|
|
token := &internal.RefreshToken{
|
|
|
|
RefreshId: refresh.ID,
|
|
|
|
Token: refresh.Token,
|
|
|
|
}
|
|
|
|
if refreshToken, err = internal.Marshal(token); err != nil {
|
|
|
|
s.logger.Errorf("failed to marshal refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2016-12-23 00:41:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
if err := s.storage.CreateRefresh(refresh); err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to create refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2017-02-01 00:11:59 +00:00
|
|
|
|
|
|
|
// deleteToken determines if we need to delete the newly created refresh token
|
|
|
|
// due to a failure in updating/creating the OfflineSession object for the
|
|
|
|
// corresponding user.
|
|
|
|
var deleteToken bool
|
|
|
|
defer func() {
|
|
|
|
if deleteToken {
|
|
|
|
// Delete newly created refresh token from storage.
|
|
|
|
if err := s.storage.DeleteRefresh(refresh.ID); err != nil {
|
|
|
|
s.logger.Errorf("failed to delete refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tokenRef := storage.RefreshTokenRef{
|
|
|
|
ID: refresh.ID,
|
|
|
|
ClientID: refresh.ClientID,
|
|
|
|
CreatedAt: refresh.CreatedAt,
|
|
|
|
LastUsed: refresh.LastUsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to retrieve an existing OfflineSession object for the corresponding user.
|
|
|
|
if session, err := s.storage.GetOfflineSessions(refresh.Claims.UserID, refresh.ConnectorID); err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
|
|
|
s.logger.Errorf("failed to get offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2017-02-01 00:11:59 +00:00
|
|
|
}
|
|
|
|
offlineSessions := storage.OfflineSessions{
|
|
|
|
UserID: refresh.Claims.UserID,
|
|
|
|
ConnID: refresh.ConnectorID,
|
|
|
|
Refresh: make(map[string]*storage.RefreshTokenRef),
|
|
|
|
}
|
|
|
|
offlineSessions.Refresh[tokenRef.ClientID] = &tokenRef
|
|
|
|
|
|
|
|
// Create a new OfflineSession object for the user and add a reference object for
|
2017-03-20 16:16:56 +00:00
|
|
|
// the newly received refreshtoken.
|
2017-02-01 00:11:59 +00:00
|
|
|
if err := s.storage.CreateOfflineSessions(offlineSessions); err != nil {
|
|
|
|
s.logger.Errorf("failed to create offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2017-02-01 00:11:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if oldTokenRef, ok := session.Refresh[tokenRef.ClientID]; ok {
|
|
|
|
// Delete old refresh token from storage.
|
2020-03-12 22:21:56 +00:00
|
|
|
if err := s.storage.DeleteRefresh(oldTokenRef.ID); err != nil && err != storage.ErrNotFound {
|
2017-02-01 00:11:59 +00:00
|
|
|
s.logger.Errorf("failed to delete refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2017-02-01 00:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update existing OfflineSession obj with new RefreshTokenRef.
|
|
|
|
if err := s.storage.UpdateOfflineSessions(session.UserID, session.ConnID, func(old storage.OfflineSessions) (storage.OfflineSessions, error) {
|
|
|
|
old.Refresh[tokenRef.ClientID] = &tokenRef
|
|
|
|
return old, nil
|
|
|
|
}); err != nil {
|
|
|
|
s.logger.Errorf("failed to update offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
2020-01-28 19:14:30 +00:00
|
|
|
return nil, err
|
2017-02-01 00:11:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2020-01-28 19:14:30 +00:00
|
|
|
return s.toAccessTokenResponse(idToken, accessToken, refreshToken, expiry), nil
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:17:39 +00:00
|
|
|
func (s *Server) handleUserInfo(w http.ResponseWriter, r *http.Request) {
|
2019-06-20 17:15:59 +00:00
|
|
|
const prefix = "Bearer "
|
2019-05-27 07:17:39 +00:00
|
|
|
|
2019-06-20 17:15:59 +00:00
|
|
|
auth := r.Header.Get("authorization")
|
|
|
|
if len(auth) < len(prefix) || !strings.EqualFold(prefix, auth[:len(prefix)]) {
|
|
|
|
w.Header().Set("WWW-Authenticate", "Bearer")
|
|
|
|
s.tokenErrHelper(w, errAccessDenied, "Invalid bearer token.", http.StatusUnauthorized)
|
2019-05-27 07:17:39 +00:00
|
|
|
return
|
|
|
|
}
|
2019-06-20 17:15:59 +00:00
|
|
|
rawIDToken := auth[len(prefix):]
|
2019-05-27 07:17:39 +00:00
|
|
|
|
2019-06-20 17:15:59 +00:00
|
|
|
verifier := oidc.NewVerifier(s.issuerURL.String(), &storageKeySet{s.storage}, &oidc.Config{SkipClientIDCheck: true})
|
|
|
|
idToken, err := verifier.Verify(r.Context(), rawIDToken)
|
2019-05-27 07:17:39 +00:00
|
|
|
if err != nil {
|
2019-06-20 17:15:59 +00:00
|
|
|
s.tokenErrHelper(w, errAccessDenied, err.Error(), http.StatusForbidden)
|
2019-05-27 07:17:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-20 17:15:59 +00:00
|
|
|
var claims json.RawMessage
|
|
|
|
if err := idToken.Claims(&claims); err != nil {
|
|
|
|
s.tokenErrHelper(w, errServerError, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2019-05-27 07:17:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 17:15:59 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Write(claims)
|
2019-05-27 07:17:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-03 03:15:01 +00:00
|
|
|
func (s *Server) handlePasswordGrant(w http.ResponseWriter, r *http.Request, client storage.Client) {
|
|
|
|
// Parse the fields
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "Couldn't parse data", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
q := r.Form
|
|
|
|
|
|
|
|
nonce := q.Get("nonce")
|
|
|
|
// Some clients, like the old go-oidc, provide extra whitespace. Tolerate this.
|
|
|
|
scopes := strings.Fields(q.Get("scope"))
|
|
|
|
|
|
|
|
// Parse the scopes if they are passed
|
|
|
|
var (
|
|
|
|
unrecognized []string
|
|
|
|
invalidScopes []string
|
|
|
|
)
|
|
|
|
hasOpenIDScope := false
|
|
|
|
for _, scope := range scopes {
|
|
|
|
switch scope {
|
|
|
|
case scopeOpenID:
|
|
|
|
hasOpenIDScope = true
|
|
|
|
case scopeOfflineAccess, scopeEmail, scopeProfile, scopeGroups, scopeFederatedID:
|
|
|
|
default:
|
|
|
|
peerID, ok := parseCrossClientScope(scope)
|
|
|
|
if !ok {
|
|
|
|
unrecognized = append(unrecognized, scope)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:39:08 +00:00
|
|
|
isTrusted, err := s.validateCrossClientTrust(client.ID, peerID)
|
2018-01-03 03:15:01 +00:00
|
|
|
if err != nil {
|
|
|
|
s.tokenErrHelper(w, errInvalidClient, fmt.Sprintf("Error validating cross client trust %v.", err), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isTrusted {
|
|
|
|
invalidScopes = append(invalidScopes, scope)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !hasOpenIDScope {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, `Missing required scope(s) ["openid"].`, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(unrecognized) > 0 {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, fmt.Sprintf("Unrecognized scope(s) %q", unrecognized), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(invalidScopes) > 0 {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, fmt.Sprintf("Client can't request scope(s) %q", invalidScopes), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Which connector
|
|
|
|
connID := s.passwordConnector
|
|
|
|
conn, err := s.getConnector(connID)
|
|
|
|
if err != nil {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "Requested connector does not exist.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordConnector, ok := conn.Connector.(connector.PasswordConnector)
|
|
|
|
if !ok {
|
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "Requested password connector does not correct type.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login
|
|
|
|
username := q.Get("username")
|
|
|
|
password := q.Get("password")
|
|
|
|
identity, ok, err := passwordConnector.Login(r.Context(), parseScopes(scopes), username, password)
|
|
|
|
if err != nil {
|
2020-10-04 04:55:08 +00:00
|
|
|
s.logger.Errorf("Failed to login user: %v", err)
|
2018-01-03 03:15:01 +00:00
|
|
|
s.tokenErrHelper(w, errInvalidRequest, "Could not login user", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
s.tokenErrHelper(w, errAccessDenied, "Invalid username or password", http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the claims to send the id token
|
|
|
|
claims := storage.Claims{
|
|
|
|
UserID: identity.UserID,
|
|
|
|
Username: identity.Username,
|
|
|
|
PreferredUsername: identity.PreferredUsername,
|
|
|
|
Email: identity.Email,
|
|
|
|
EmailVerified: identity.EmailVerified,
|
|
|
|
Groups: identity.Groups,
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:41:11 +00:00
|
|
|
accessToken, err := s.newAccessToken(client.ID, claims, scopes, nonce, connID)
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Errorf("password grant failed to create new access token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-28 12:01:40 +00:00
|
|
|
idToken, expiry, err := s.newIDToken(client.ID, claims, scopes, nonce, accessToken, "", connID)
|
2018-01-03 03:15:01 +00:00
|
|
|
if err != nil {
|
2021-08-11 18:41:11 +00:00
|
|
|
s.logger.Errorf("password grant failed to create new ID token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2018-01-03 03:15:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
reqRefresh := func() bool {
|
|
|
|
// Ensure the connector supports refresh tokens.
|
|
|
|
//
|
|
|
|
// Connectors like `saml` do not implement RefreshConnector.
|
|
|
|
_, ok := conn.Connector.(connector.RefreshConnector)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, scope := range scopes {
|
|
|
|
if scope == scopeOfflineAccess {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
var refreshToken string
|
|
|
|
if reqRefresh {
|
|
|
|
refresh := storage.RefreshToken{
|
|
|
|
ID: storage.NewID(),
|
|
|
|
Token: storage.NewID(),
|
2020-01-10 19:39:08 +00:00
|
|
|
ClientID: client.ID,
|
2018-01-03 03:15:01 +00:00
|
|
|
ConnectorID: connID,
|
|
|
|
Scopes: scopes,
|
|
|
|
Claims: claims,
|
|
|
|
Nonce: nonce,
|
|
|
|
// ConnectorData: authCode.ConnectorData,
|
|
|
|
CreatedAt: s.now(),
|
|
|
|
LastUsed: s.now(),
|
|
|
|
}
|
|
|
|
token := &internal.RefreshToken{
|
|
|
|
RefreshId: refresh.ID,
|
|
|
|
Token: refresh.Token,
|
|
|
|
}
|
|
|
|
if refreshToken, err = internal.Marshal(token); err != nil {
|
|
|
|
s.logger.Errorf("failed to marshal refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.storage.CreateRefresh(refresh); err != nil {
|
|
|
|
s.logger.Errorf("failed to create refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteToken determines if we need to delete the newly created refresh token
|
|
|
|
// due to a failure in updating/creating the OfflineSession object for the
|
|
|
|
// corresponding user.
|
|
|
|
var deleteToken bool
|
|
|
|
defer func() {
|
|
|
|
if deleteToken {
|
|
|
|
// Delete newly created refresh token from storage.
|
|
|
|
if err := s.storage.DeleteRefresh(refresh.ID); err != nil {
|
|
|
|
s.logger.Errorf("failed to delete refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
tokenRef := storage.RefreshTokenRef{
|
|
|
|
ID: refresh.ID,
|
|
|
|
ClientID: refresh.ClientID,
|
|
|
|
CreatedAt: refresh.CreatedAt,
|
|
|
|
LastUsed: refresh.LastUsed,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to retrieve an existing OfflineSession object for the corresponding user.
|
|
|
|
if session, err := s.storage.GetOfflineSessions(refresh.Claims.UserID, refresh.ConnectorID); err != nil {
|
|
|
|
if err != storage.ErrNotFound {
|
|
|
|
s.logger.Errorf("failed to get offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
offlineSessions := storage.OfflineSessions{
|
2021-07-20 20:05:35 +00:00
|
|
|
UserID: refresh.Claims.UserID,
|
|
|
|
ConnID: refresh.ConnectorID,
|
|
|
|
Refresh: make(map[string]*storage.RefreshTokenRef),
|
|
|
|
ConnectorData: identity.ConnectorData,
|
2018-01-03 03:15:01 +00:00
|
|
|
}
|
|
|
|
offlineSessions.Refresh[tokenRef.ClientID] = &tokenRef
|
|
|
|
|
|
|
|
// Create a new OfflineSession object for the user and add a reference object for
|
|
|
|
// the newly received refreshtoken.
|
|
|
|
if err := s.storage.CreateOfflineSessions(offlineSessions); err != nil {
|
|
|
|
s.logger.Errorf("failed to create offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if oldTokenRef, ok := session.Refresh[tokenRef.ClientID]; ok {
|
|
|
|
// Delete old refresh token from storage.
|
|
|
|
if err := s.storage.DeleteRefresh(oldTokenRef.ID); err != nil {
|
2020-03-25 12:43:53 +00:00
|
|
|
if err == storage.ErrNotFound {
|
|
|
|
s.logger.Warnf("database inconsistent, refresh token missing: %v", oldTokenRef.ID)
|
|
|
|
} else {
|
|
|
|
s.logger.Errorf("failed to delete refresh token: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
|
|
|
return
|
|
|
|
}
|
2018-01-03 03:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update existing OfflineSession obj with new RefreshTokenRef.
|
|
|
|
if err := s.storage.UpdateOfflineSessions(session.UserID, session.ConnID, func(old storage.OfflineSessions) (storage.OfflineSessions, error) {
|
|
|
|
old.Refresh[tokenRef.ClientID] = &tokenRef
|
2021-07-20 20:05:35 +00:00
|
|
|
old.ConnectorData = identity.ConnectorData
|
2018-01-03 03:15:01 +00:00
|
|
|
return old, nil
|
|
|
|
}); err != nil {
|
|
|
|
s.logger.Errorf("failed to update offline session: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
|
|
|
deleteToken = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 15:07:18 +00:00
|
|
|
resp := s.toAccessTokenResponse(idToken, accessToken, refreshToken, expiry)
|
|
|
|
s.writeAccessToken(w, resp)
|
2018-01-03 03:15:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 03:01:51 +00:00
|
|
|
type accessTokenResponse struct {
|
2020-01-28 19:14:30 +00:00
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
ExpiresIn int `json:"expires_in"`
|
|
|
|
RefreshToken string `json:"refresh_token,omitempty"`
|
|
|
|
IDToken string `json:"id_token"`
|
|
|
|
}
|
|
|
|
|
2020-12-20 03:01:51 +00:00
|
|
|
func (s *Server) toAccessTokenResponse(idToken, accessToken, refreshToken string, expiry time.Time) *accessTokenResponse {
|
|
|
|
return &accessTokenResponse{
|
2017-01-11 01:51:12 +00:00
|
|
|
accessToken,
|
2016-07-25 20:00:28 +00:00
|
|
|
"bearer",
|
2016-11-04 04:39:31 +00:00
|
|
|
int(expiry.Sub(s.now()).Seconds()),
|
2016-07-25 20:00:28 +00:00
|
|
|
refreshToken,
|
|
|
|
idToken,
|
|
|
|
}
|
2020-01-28 19:14:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 03:01:51 +00:00
|
|
|
func (s *Server) writeAccessToken(w http.ResponseWriter, resp *accessTokenResponse) {
|
2016-07-25 20:00:28 +00:00
|
|
|
data, err := json.Marshal(resp)
|
|
|
|
if err != nil {
|
2016-12-12 22:54:01 +00:00
|
|
|
s.logger.Errorf("failed to marshal access token response: %v", err)
|
|
|
|
s.tokenErrHelper(w, errServerError, "", http.StatusInternalServerError)
|
2016-07-25 20:00:28 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(data)))
|
2021-01-18 07:10:00 +00:00
|
|
|
|
|
|
|
// Token response must include cache headers https://tools.ietf.org/html/rfc6749#section-5.1
|
|
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
|
|
w.Header().Set("Pragma", "no-cache")
|
2016-07-25 20:00:28 +00:00
|
|
|
w.Write(data)
|
|
|
|
}
|
|
|
|
|
2019-09-27 13:56:32 +00:00
|
|
|
func (s *Server) renderError(r *http.Request, w http.ResponseWriter, status int, description string) {
|
|
|
|
if err := s.templates.err(r, w, status, description); err != nil {
|
2016-12-14 22:17:59 +00:00
|
|
|
s.logger.Errorf("Server template error: %v", err)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-12-12 22:54:01 +00:00
|
|
|
|
|
|
|
func (s *Server) tokenErrHelper(w http.ResponseWriter, typ string, description string, statusCode int) {
|
|
|
|
if err := tokenErr(w, typ, description, statusCode); err != nil {
|
2017-03-20 16:16:56 +00:00
|
|
|
s.logger.Errorf("token error response: %v", err)
|
2016-12-12 22:54:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-07 09:28:21 +00:00
|
|
|
|
|
|
|
// Check for username prompt override from connector. Defaults to "Username".
|
|
|
|
func usernamePrompt(conn connector.PasswordConnector) string {
|
|
|
|
if attr := conn.Prompt(); attr != "" {
|
|
|
|
return attr
|
|
|
|
}
|
|
|
|
return "Username"
|
|
|
|
}
|