github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/respond_qrcode.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/util"
    21  	addrutil "github.com/greenpau/go-authcrunch/pkg/util/addr"
    22  	"github.com/skip2/go-qrcode"
    23  	"go.uber.org/zap"
    24  	"net/http"
    25  )
    26  
    27  func (p *Portal) handleQRCode(ctx context.Context, w http.ResponseWriter, r *http.Request, rr *requests.Request) error {
    28  	p.injectSessionID(ctx, w, r, rr)
    29  	p.logger.Debug(
    30  		"Received QR code request",
    31  		zap.String("session_id", rr.Upstream.SessionID),
    32  		zap.String("request_id", rr.ID),
    33  		zap.String("src_ip", addrutil.GetSourceAddress(r)),
    34  		zap.String("src_conn_ip", addrutil.GetSourceConnAddress(r)),
    35  	)
    36  	w.Header().Set("Content-Type", "image/png")
    37  
    38  	png, err := qrcode.Encode(util.GetCurrentBaseURL(r)+"/login", qrcode.Medium, 256)
    39  	if err != nil {
    40  		return p.handleHTTPRenderPlainText(ctx, w, http.StatusInternalServerError)
    41  	}
    42  	w.Write(png)
    43  	return nil
    44  }