2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2023-07-29 15:46:33 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2023-07-29 15:46:33 +00:00
|
|
|
"fmt"
|
2021-09-11 11:37:31 +00:00
|
|
|
"net/http"
|
2023-07-30 14:48:26 +00:00
|
|
|
"time"
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2021-09-11 11:37:31 +00:00
|
|
|
"github.com/lestrrat-go/jwx/jwk"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2023-07-29 15:53:26 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/internal/services"
|
2021-09-11 11:37:31 +00:00
|
|
|
)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
type OidcCallbackHandler struct {
|
2021-09-11 11:37:31 +00:00
|
|
|
keySet jwk.Set
|
|
|
|
logger *log.Logger
|
|
|
|
oauth2Config *oauth2.Config
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func (c *OidcCallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-09-11 11:37:31 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
if r.URL.Path != "/callback" {
|
|
|
|
http.NotFound(w, r)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errorText := r.URL.Query().Get("error")
|
|
|
|
errorDescription := r.URL.Query().Get("error_description")
|
|
|
|
code := r.URL.Query().Get("code")
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
if c.handleCallbackError(errorText, errorDescription, r) {
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
tok, err := c.oauth2Config.Exchange(r.Context(), code)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
c.logger.WithError(err).Error("could not perform token exchange")
|
2021-09-11 11:37:31 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
session, err := GetSession(r)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2023-08-03 14:46:28 +00:00
|
|
|
c.logger.WithError(err).Error("could not get session")
|
2023-07-29 15:46:33 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
if err = c.storeTokens(session, tok); err != nil {
|
|
|
|
c.logger.WithError(err).Error("could not store token in session")
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = session.Save(r, w); err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
c.logger.WithError(err).Error("could not save session")
|
|
|
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
if redirectTarget, ok := session.Values[services.SessionRedirectTarget]; ok {
|
2023-07-29 15:46:33 +00:00
|
|
|
if v, ok := redirectTarget.(string); ok {
|
|
|
|
w.Header().Set("Location", v)
|
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
w.Header().Set("Location", "/")
|
2021-09-11 11:37:31 +00:00
|
|
|
w.WriteHeader(http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func (c *OidcCallbackHandler) handleCallbackError(errorText string, errorDescription string, r *http.Request) bool {
|
|
|
|
if errorText != "" {
|
|
|
|
errorDetails := &ErrorDetails{
|
|
|
|
ErrorMessage: errorText,
|
|
|
|
}
|
|
|
|
|
|
|
|
if errorDescription != "" {
|
|
|
|
errorDetails.ErrorDetails = []string{errorDescription}
|
|
|
|
}
|
|
|
|
|
|
|
|
GetErrorBucket(r).AddError(errorDetails)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *OidcCallbackHandler) storeTokens(
|
|
|
|
session *sessions.Session,
|
|
|
|
tok *oauth2.Token,
|
|
|
|
) error {
|
2023-08-03 14:46:28 +00:00
|
|
|
session.Values[services.SessionAccessToken] = tok.AccessToken
|
|
|
|
session.Values[services.SessionRefreshToken] = tok.RefreshToken
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
idTokenValue := tok.Extra("id_token")
|
|
|
|
|
|
|
|
idToken, ok := idTokenValue.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("ID token value %v is not a string", idTokenValue)
|
|
|
|
}
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
session.Values[services.SessionIDToken] = idToken
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2023-07-30 14:48:26 +00:00
|
|
|
session.Options.MaxAge = int(time.Until(tok.Expiry).Seconds())
|
|
|
|
|
2023-08-03 14:46:28 +00:00
|
|
|
oidcToken, err := services.ParseIDToken(idToken, c.keySet)
|
2023-07-29 15:46:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not parse ID token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.logger.WithFields(log.Fields{
|
|
|
|
"sub": oidcToken.Subject(),
|
|
|
|
"aud": oidcToken.Audience(),
|
|
|
|
"issued_at": oidcToken.IssuedAt(),
|
|
|
|
"iss": oidcToken.Issuer(),
|
|
|
|
"not_before": oidcToken.NotBefore(),
|
|
|
|
"exp": oidcToken.Expiration(),
|
|
|
|
}).Debug("receive OpenID Connect ID Token")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCallbackHandler(logger *log.Logger, keySet jwk.Set, oauth2Config *oauth2.Config) *OidcCallbackHandler {
|
|
|
|
return &OidcCallbackHandler{
|
|
|
|
keySet: keySet,
|
2021-09-11 11:37:31 +00:00
|
|
|
logger: logger,
|
2023-07-29 15:46:33 +00:00
|
|
|
oauth2Config: oauth2Config,
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
}
|