github.com/greenpau/go-authcrunch@v1.0.50/pkg/authn/handle_http_settings_gpgkeys.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/json"
    20  	"fmt"
    21  	"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator"
    22  	"github.com/greenpau/go-authcrunch/pkg/identity"
    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  	"net/http"
    27  	"strings"
    28  )
    29  
    30  func (p *Portal) handleHTTPGPGKeysSettings(
    31  	ctx context.Context, r *http.Request, rr *requests.Request,
    32  	usr *user.User, store ids.IdentityStore, data map[string]interface{},
    33  ) error {
    34  	var action string
    35  	var status bool
    36  	entrypoint := "gpgkeys"
    37  	data["view"] = entrypoint
    38  	endpoint, err := getEndpoint(r.URL.Path, "/"+entrypoint)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	switch {
    43  	case strings.HasPrefix(endpoint, "/add") && r.Method == "POST":
    44  		// Add GPG key.
    45  		action = "add"
    46  		status = true
    47  		if err := validateKeyInputForm(r, rr); err != nil {
    48  			attachFailStatus(data, "Bad Request")
    49  			break
    50  		}
    51  		rr.Key.Usage = "gpg"
    52  		if err = store.Request(operator.AddKeyGPG, rr); err != nil {
    53  			attachFailStatus(data, fmt.Sprintf("%v", err))
    54  			break
    55  		}
    56  		attachSuccessStatus(data, "Public GPG key has been added")
    57  	case strings.HasPrefix(endpoint, "/add"):
    58  		action = "add"
    59  	case strings.HasPrefix(endpoint, "/delete"):
    60  		// Delete a particular GPG key.
    61  		action = "delete"
    62  		status = true
    63  		keyID, err := getEndpointKeyID(endpoint, "/delete/")
    64  		if err != nil {
    65  			attachFailStatus(data, fmt.Sprintf("%v", err))
    66  			break
    67  		}
    68  		rr.Key.ID = keyID
    69  		if err = store.Request(operator.DeletePublicKey, rr); err != nil {
    70  			attachFailStatus(data, fmt.Sprintf("failed deleting key id %s: %v", keyID, err))
    71  			break
    72  		}
    73  		attachSuccessStatus(data, fmt.Sprintf("key id %s deleted successfully", keyID))
    74  	case strings.HasPrefix(endpoint, "/view"):
    75  		// Get a particular GPG key.
    76  		action = "view"
    77  		keyID, err := getEndpointKeyID(endpoint, "/view/")
    78  		if err != nil {
    79  			attachFailStatus(data, fmt.Sprintf("%v", err))
    80  			break
    81  		}
    82  		rr.Key.Usage = "gpg"
    83  		if err = store.Request(operator.GetPublicKeys, rr); err != nil {
    84  			attachFailStatus(data, fmt.Sprintf("failed fetching key id %s: %v", keyID, err))
    85  			break
    86  		}
    87  		bundle := rr.Response.Payload.(*identity.PublicKeyBundle)
    88  		for _, k := range bundle.Get() {
    89  			if k.ID != keyID {
    90  				continue
    91  			}
    92  			var keyMap map[string]interface{}
    93  			keyBytes, _ := json.Marshal(k)
    94  			json.Unmarshal(keyBytes, &keyMap)
    95  			for _, w := range []string{"payload"} {
    96  				if _, exists := keyMap[w]; !exists {
    97  					continue
    98  				}
    99  				delete(keyMap, w)
   100  			}
   101  			prettyKey, _ := json.MarshalIndent(keyMap, "", "  ")
   102  			attachSuccessStatus(data, "OK")
   103  			data["key"] = string(prettyKey)
   104  			if k.Payload != "" {
   105  				data["pem_key"] = k.Payload
   106  			}
   107  			break
   108  		}
   109  	default:
   110  		// List GPG Keys.
   111  		rr.Key.Usage = "gpg"
   112  		if err = store.Request(operator.GetPublicKeys, rr); err != nil {
   113  			attachFailStatus(data, fmt.Sprintf("%v", err))
   114  			break
   115  		}
   116  		bundle := rr.Response.Payload.(*identity.PublicKeyBundle)
   117  		pubKeys := bundle.Get()
   118  		if len(pubKeys) > 0 {
   119  			data[entrypoint] = pubKeys
   120  		}
   121  	}
   122  	attachView(data, entrypoint, action, status)
   123  	return nil
   124  }