github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/api_fetch_user_uni_sec_factor_reg_params.go (about)

     1  // Copyright 2024 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/ids"
    24  	"github.com/greenpau/go-authcrunch/pkg/requests"
    25  	"github.com/greenpau/go-authcrunch/pkg/user"
    26  	"github.com/greenpau/go-authcrunch/pkg/util"
    27  )
    28  
    29  // FetchUserUniSecFactorRegParams fetches U2F authenticator registration parameters.
    30  func (p *Portal) FetchUserUniSecFactorRegParams(
    31  	ctx context.Context,
    32  	w http.ResponseWriter,
    33  	r *http.Request,
    34  	rr *requests.Request,
    35  	parsedUser *user.User,
    36  	resp map[string]interface{},
    37  	usr *user.User,
    38  	backend ids.IdentityStore,
    39  	bodyData map[string]interface{}) error {
    40  
    41  	params := make(map[string]interface{})
    42  	randomStr := util.GetRandomStringFromRange(64, 92)
    43  	params["challenge"] = strings.TrimRight(base64.StdEncoding.EncodeToString([]byte(randomStr)), "=")
    44  	params["rp_name"] = "AuthCrunch"
    45  	// params["rp_id"] = "auth.authcrunch.com"
    46  	params["user_id"] = usr.Claims.ID
    47  	params["user_name"] = usr.Claims.Email
    48  	params["user_verification"] = "discouraged"
    49  	params["attestation"] = "direct"
    50  	if usr.Claims.Name == "" {
    51  		params["user_display_name"] = usr.Claims.Subject
    52  	} else {
    53  		params["user_display_name"] = usr.Claims.Name
    54  	}
    55  
    56  	resp["entry"] = params
    57  	return handleAPIProfileResponse(w, rr, http.StatusOK, resp)
    58  }