95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"ruben/inventory2/domains/authentication"
|
|
"ruben/inventory2/logging"
|
|
"ruben/inventory2/server/cookies"
|
|
"ruben/inventory2/server/response"
|
|
)
|
|
|
|
type loginSubrouter struct {
|
|
log *logging.Logger
|
|
auth *authentication.Authenticator
|
|
}
|
|
|
|
func Routes(
|
|
r *gin.RouterGroup,
|
|
logger *logging.Logger,
|
|
auth *authentication.Authenticator,
|
|
) {
|
|
ls := &loginSubrouter{
|
|
log: logger,
|
|
auth: auth,
|
|
}
|
|
|
|
r.GET("/login", response.Handler(ls.loginPage))
|
|
r.GET("/login/callback", response.Handler(ls.loginCallback))
|
|
r.GET("/logout", response.Handler(ls.logoutPage))
|
|
}
|
|
|
|
func (s *loginSubrouter) loginPage(c *gin.Context) (response.Response, error) {
|
|
r := c.Request
|
|
ctx := r.Context()
|
|
|
|
u, err := NewLoginURL(ctx, s.auth, "/")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return response.TemporaryRedirect(u), nil
|
|
}
|
|
|
|
func NewLoginURL(ctx context.Context, auth *authentication.Authenticator, targetURI string) (string, error) {
|
|
state, err := auth.NewState(ctx, targetURI)
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to generate random state: %w", err)
|
|
}
|
|
|
|
base64EncodedState := fmt.Sprintf("%x", state[:])
|
|
|
|
return auth.AuthCodeURL(base64EncodedState), nil
|
|
}
|
|
|
|
func (s *loginSubrouter) loginCallback(c *gin.Context) (response.Response, error) {
|
|
r := c.Request
|
|
ctx := r.Context()
|
|
q := r.URL.Query()
|
|
|
|
// obtain token and profile
|
|
|
|
accessToken, targetURI, expiration, err := s.auth.Exchange(ctx, q.Get("state"), q.Get("code"))
|
|
if err != nil {
|
|
return nil, response.Unauthorized().
|
|
Msg(fmt.Sprintf("Failed to exchange an authorization code for a token")).
|
|
Wrap(err)
|
|
}
|
|
|
|
// set access_token cookie and redirect to a reasonable place
|
|
|
|
return response.TemporaryRedirect(targetURI).
|
|
Cookie(cookies.AccessToken(accessToken, expiration)), nil
|
|
}
|
|
|
|
func (s *loginSubrouter) logoutPage(c *gin.Context) (response.Response, error) {
|
|
r := c.Request
|
|
|
|
host := r.Header.Get("X-Forwarded-Host")
|
|
if host == "" {
|
|
host = r.Host
|
|
}
|
|
|
|
if ck, err := r.Cookie("access_token"); err == nil && ck != nil {
|
|
if err := s.auth.DeleteOAuthTokens(r.Context(), ck.Value); err != nil {
|
|
s.log.Error("failed to delete auth token", "error", err)
|
|
}
|
|
}
|
|
|
|
return response.TemporaryRedirect(s.auth.GetLogoutURL(host).String()).
|
|
Cookie(cookies.Expired("access_token")), nil
|
|
}
|