2024-10-06 20:11:58 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/helpers"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/storage"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/zitadel/oidc/v3/pkg/client/rp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
const AuthRedirectRoute = "/perform_auth"
|
|
|
|
|
|
|
|
type AuthRedirectController struct {
|
2024-10-16 19:42:39 +00:00
|
|
|
provider rp.RelyingParty
|
|
|
|
l *zap.SugaredLogger
|
|
|
|
st *storage.Storage
|
2024-10-06 20:11:58 +00:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:42:39 +00:00
|
|
|
func NewAuthRedirectController(l *zap.SugaredLogger, provider rp.RelyingParty, storage *storage.Storage) *AuthRedirectController {
|
2024-10-06 20:11:58 +00:00
|
|
|
return &AuthRedirectController{
|
2024-10-16 19:42:39 +00:00
|
|
|
l: l,
|
|
|
|
st: storage,
|
|
|
|
provider: provider,
|
2024-10-06 20:11:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *AuthRedirectController) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestIDStr := r.URL.Query().Get("request_id")
|
|
|
|
if requestIDStr == "" {
|
|
|
|
helpers.HandleResponse(w, r, http.StatusBadRequest, []byte("no request ID in request"), c.l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestID, err := uuid.Parse(requestIDStr)
|
|
|
|
if err != nil {
|
|
|
|
c.l.Errorf("Invalid UUID format for request ID: %s", err)
|
|
|
|
helpers.HandleResponse(w, r, http.StatusBadRequest, []byte("invalid request id"), c.l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-16 19:42:39 +00:00
|
|
|
_, err = c.st.LocalStorage.AuthRequestStorage().GetAuthRequestByID(r.Context(), requestID)
|
2024-10-06 20:11:58 +00:00
|
|
|
if err != nil {
|
|
|
|
c.l.Errorf("Failed to get auth request from DB: %s", err)
|
|
|
|
helpers.HandleResponse(w, r, http.StatusBadRequest, []byte("unknown request id"), c.l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-16 19:42:39 +00:00
|
|
|
// backend, err := c.st.LocalStorage.BackendStorage().GetBackendByID(r.Context(), req.BackendID)
|
|
|
|
// if err != nil {
|
|
|
|
// c.l.Errorf("Failed to get backend from DB: %s", err)
|
|
|
|
// helpers.HandleResponse(w, r, http.StatusInternalServerError, []byte("failed to perform auth"), c.l)
|
|
|
|
// return
|
|
|
|
// }
|
2024-10-06 20:11:58 +00:00
|
|
|
|
2024-10-16 19:42:39 +00:00
|
|
|
// provider, err := rp.NewRelyingPartyOIDC(r.Context(), backend.Config.Issuer, backend.Config.ClientID, backend.Config.ClientSecret, backend.Config.RedirectURI, req.Scopes)
|
|
|
|
// if err != nil {
|
|
|
|
// c.l.Errorf("Failed to init relying party: %s", err)
|
|
|
|
// helpers.HandleResponse(w, r, http.StatusInternalServerError, []byte("failed to perform auth"), c.l)
|
|
|
|
// return
|
|
|
|
// }
|
2024-10-06 20:11:58 +00:00
|
|
|
|
2024-10-16 19:42:39 +00:00
|
|
|
rp.AuthURLHandler(func() string { return requestIDStr }, c.provider).ServeHTTP(w, r)
|
2024-10-06 20:11:58 +00:00
|
|
|
}
|