github.com/greenpau/go-authcrunch@v1.0.50/pkg/authn/handle_http_portal.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package authn
    16  
    17  import (
    18  	"context"
    19  	"github.com/greenpau/go-authcrunch/pkg/requests"
    20  	"github.com/greenpau/go-authcrunch/pkg/user"
    21  	addrutil "github.com/greenpau/go-authcrunch/pkg/util/addr"
    22  	"go.uber.org/zap"
    23  	"net/http"
    24  	"net/url"
    25  )
    26  
    27  func (p *Portal) handleHTTPPortal(ctx context.Context, w http.ResponseWriter, r *http.Request, rr *requests.Request, parsedUser *user.User) error {
    28  	p.disableClientCache(w)
    29  	p.injectRedirectURL(ctx, w, r, rr)
    30  	if parsedUser == nil {
    31  		return p.handleHTTPRedirect(ctx, w, r, rr, "/login")
    32  	}
    33  	usr, err := p.sessions.Get(parsedUser.Claims.ID)
    34  	if err != nil {
    35  		p.deleteAuthCookies(w, r)
    36  		p.logger.Debug(
    37  			"User session not found, redirect to login",
    38  			zap.String("session_id", rr.Upstream.SessionID),
    39  			zap.String("request_id", rr.ID),
    40  			zap.Any("user", parsedUser.Claims),
    41  			zap.Error(err),
    42  		)
    43  		return p.handleHTTPRedirect(ctx, w, r, rr, "/login")
    44  	}
    45  	return p.handleHTTPPortalScreen(ctx, w, r, rr, usr)
    46  }
    47  
    48  func (p *Portal) handleHTTPPortalScreen(ctx context.Context, w http.ResponseWriter, r *http.Request, rr *requests.Request, usr *user.User) error {
    49  	if cookie, err := r.Cookie(p.cookie.Referer); err == nil {
    50  		redirectURL, err := url.Parse(cookie.Value)
    51  		if err == nil {
    52  			p.logger.Debug(
    53  				"Cookie-based redirect",
    54  				zap.String("session_id", rr.Upstream.SessionID),
    55  				zap.String("request_id", rr.ID),
    56  				zap.String("redirect_url", redirectURL.String()),
    57  			)
    58  			w.Header().Set("Location", redirectURL.String())
    59  			w.Header().Add("Set-Cookie", p.cookie.GetDeleteCookie(addrutil.GetSourceHost(r), p.cookie.Referer))
    60  			w.WriteHeader(http.StatusSeeOther)
    61  			return nil
    62  		}
    63  	}
    64  	resp := p.ui.GetArgs()
    65  	resp.BaseURL(rr.Upstream.BasePath)
    66  	resp.PageTitle = "Applications"
    67  	if len(usr.FrontendLinks) > 0 {
    68  		// Add additional frontend links.
    69  		resp.AddFrontendLinks(usr.FrontendLinks)
    70  	}
    71  	content, err := p.ui.Render("portal", resp)
    72  	if err != nil {
    73  		return p.handleHTTPRenderError(ctx, w, r, rr, err)
    74  	}
    75  	return p.handleHTTPRenderHTML(ctx, w, http.StatusOK, content.Bytes())
    76  }