diff --git a/docker-compose.yml b/docker-compose.yml index 9576df3..cee1794 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,6 +1,6 @@ services: kotosoft_id_backend: - image: localhost:5000/kotosoft_id_backend:latest + image: 172.17.0.1:5000/kotosoft_id_backend:latest restart: unless-stopped environment: SERVER_ADDR: ":8080" @@ -26,7 +26,7 @@ services: - eu_net kotosoft_id_frontend: - image: localhost:5000/kotosoft_id_frontend:latest + image: 172.17.0.1:5000/kotosoft_id_frontend:latest restart: unless-stopped environment: PUBLIC_URL: ${FRONTEND_URL} diff --git a/kotosoft-id-backend/internal/handlers/oauth.go b/kotosoft-id-backend/internal/handlers/oauth.go index 7d073b9..37ce69f 100644 --- a/kotosoft-id-backend/internal/handlers/oauth.go +++ b/kotosoft-id-backend/internal/handlers/oauth.go @@ -58,11 +58,7 @@ func (h *OAuthHandler) SessionLogin(c *gin.Context) { redirectURL, err := h.svc.SessionLogin(c.Request.Context(), input.Session, input.Email, input.Password) if err != nil { - if err == services.ErrInvalidGrant { - oauthErrorJSON(c, services.ErrInvalidGrant) - } else { - oauthErrorJSON(c, services.ErrAccessDenied) - } + oauthErrorJSON(c, err) return } @@ -91,7 +87,7 @@ func (h *OAuthHandler) SessionAttach(c *gin.Context) { redirectURL, err := h.svc.SessionAttach(c.Request.Context(), input.Session, accessToken) if err != nil { - oauthErrorJSON(c, services.ErrAccessDenied) + oauthErrorJSON(c, err) return } @@ -114,7 +110,12 @@ func (h *OAuthHandler) SessionApprove(c *gin.Context) { } if !input.Allow { - oauthErrorJSON(c, services.ErrAccessDenied) + redirectURL, err := h.svc.SessionDeny(c.Request.Context(), input.Session) + if err != nil { + oauthErrorJSON(c, err) + return + } + c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL}) return } diff --git a/kotosoft-id-backend/internal/services/oauth.go b/kotosoft-id-backend/internal/services/oauth.go index 432b948..7dea0c8 100644 --- a/kotosoft-id-backend/internal/services/oauth.go +++ b/kotosoft-id-backend/internal/services/oauth.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "log" "net/url" "strings" "time" @@ -156,10 +157,14 @@ func (s *Service) SessionAttach(ctx context.Context, sessionToken, accessToken s } func (s *Service) SessionApprove(ctx context.Context, token string) (string, error) { - sess, err := s.GetOAuthSession(ctx, token) + data, err := s.redis.GetDel(ctx, oauthSessionPrefix+token).Bytes() if err != nil { return "", ErrInvalidGrant } + var sess OAuthSession + if err := json.Unmarshal(data, &sess); err != nil { + return "", ErrInvalidGrant + } if sess.UserID == uuid.Nil { return "", ErrInvalidGrant } @@ -174,8 +179,9 @@ func (s *Service) SessionApprove(ctx context.Context, token string) (string, err return "", err } - _ = s.redis.Del(ctx, oauthSessionPrefix+token) - _ = s.oauth.RecordUserConsent(ctx, sess.UserID, client.ID, sess.Scopes) + if err := s.oauth.RecordUserConsent(ctx, sess.UserID, client.ID, sess.Scopes); err != nil { + log.Printf("oauth: failed to record consent for user %s client %s: %v", sess.UserID, client.ClientID, err) + } redirectURL, _ := url.Parse(sess.RedirectURI) q := redirectURL.Query() @@ -188,6 +194,27 @@ func (s *Service) SessionApprove(ctx context.Context, token string) (string, err return redirectURL.String(), nil } +func (s *Service) SessionDeny(ctx context.Context, token string) (string, error) { + data, err := s.redis.GetDel(ctx, oauthSessionPrefix+token).Bytes() + if err != nil { + return "", ErrInvalidGrant + } + var sess OAuthSession + if err := json.Unmarshal(data, &sess); err != nil { + return "", ErrInvalidGrant + } + + redirectURL, _ := url.Parse(sess.RedirectURI) + q := redirectURL.Query() + q.Set("error", "access_denied") + if sess.State != "" { + q.Set("state", sess.State) + } + redirectURL.RawQuery = q.Encode() + + return redirectURL.String(), nil +} + func (s *Service) ExchangeCode(ctx context.Context, auth ClientAuth, code, redirectURI, codeVerifier string) (*TokenPair, error) { if auth.ClientID == "" || code == "" || redirectURI == "" || codeVerifier == "" { return nil, ErrInvalidRequest diff --git a/kotosoft-id-frontend/app/consent/page.tsx b/kotosoft-id-frontend/app/consent/page.tsx index 27ff079..967dafc 100644 --- a/kotosoft-id-frontend/app/consent/page.tsx +++ b/kotosoft-id-frontend/app/consent/page.tsx @@ -36,16 +36,12 @@ export default function ConsentPage() { const handleDecision = async (allow: boolean) => { setIsSubmitting(true); - if (allow) { - const result = await Approve(sessionToken, allow); - if (result) { - window.location.href = result; - } else { - setError(t("invalidSession")); - setIsSubmitting(false); - } + const result = await Approve(sessionToken, allow); + if (result) { + window.location.href = result; } else { - window.location.href = session?.redirect_uri || "/"; + setError(t("invalidSession")); + setIsSubmitting(false); } }; diff --git a/kotosoft-id-frontend/app/login/page.tsx b/kotosoft-id-frontend/app/login/page.tsx index 0eb491b..edb5204 100644 --- a/kotosoft-id-frontend/app/login/page.tsx +++ b/kotosoft-id-frontend/app/login/page.tsx @@ -35,7 +35,11 @@ export default function LoginPage() { useEffect(() => { if (redirectTo) { - router.push(redirectTo); + if (redirectTo.startsWith("http")) { + window.location.href = redirectTo; + } else { + router.push(redirectTo); + } } }, [redirectTo, router]);