github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/handle_http_barcode.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  	"encoding/base64"
    20  	"net/http"
    21  	"strings"
    22  
    23  	"github.com/greenpau/go-authcrunch/pkg/requests"
    24  	"github.com/greenpau/go-authcrunch/pkg/user"
    25  	"github.com/skip2/go-qrcode"
    26  )
    27  
    28  func (p *Portal) handleHTTPProfileMfaBarcode(ctx context.Context, w http.ResponseWriter, r *http.Request, rr *requests.Request, parsedUser *user.User) error {
    29  	p.disableClientCache(w)
    30  	p.injectRedirectURL(ctx, w, r, rr)
    31  	if parsedUser == nil {
    32  		if rr.Response.RedirectURL == "" {
    33  			return p.handleHTTPRedirect(ctx, w, r, rr, "/login?redirect_url="+r.RequestURI)
    34  		}
    35  		return p.handleHTTPRedirect(ctx, w, r, rr, "/login")
    36  	}
    37  
    38  	endpoint, err := getEndpoint(r.URL.Path, "/barcode")
    39  	if err != nil {
    40  		return p.handleHTTPError(ctx, w, r, rr, http.StatusBadRequest)
    41  	}
    42  
    43  	qrCodeEncoded := strings.TrimPrefix(endpoint, "/mfa/")
    44  	qrCodeEncoded = strings.TrimSuffix(qrCodeEncoded, ".png")
    45  	codeURI, err := base64.StdEncoding.DecodeString(qrCodeEncoded)
    46  	if err != nil {
    47  		return p.handleHTTPRenderPlainText(ctx, w, http.StatusBadRequest)
    48  	}
    49  	png, err := qrcode.Encode(string(codeURI), qrcode.Medium, 256)
    50  	if err != nil {
    51  		return p.handleHTTPRenderPlainText(ctx, w, http.StatusInternalServerError)
    52  	}
    53  	w.Header().Set("Content-Type", "image/png")
    54  	w.Write(png)
    55  	return nil
    56  }
    57  
    58  func (p *Portal) handleHTTPSandboxMfaBarcode(ctx context.Context, w http.ResponseWriter, _ *http.Request, endpoint string) error {
    59  	qrCodeEncoded := strings.TrimPrefix(endpoint, "/mfa/barcode/")
    60  	qrCodeEncoded = strings.TrimSuffix(qrCodeEncoded, ".png")
    61  	codeURI, err := base64.StdEncoding.DecodeString(qrCodeEncoded)
    62  	if err != nil {
    63  		return p.handleHTTPRenderPlainText(ctx, w, http.StatusBadRequest)
    64  	}
    65  	png, err := qrcode.Encode(string(codeURI), qrcode.Medium, 256)
    66  	if err != nil {
    67  		return p.handleHTTPRenderPlainText(ctx, w, http.StatusInternalServerError)
    68  	}
    69  	w.Header().Set("Content-Type", "image/png")
    70  	w.Write(png)
    71  	return nil
    72  }