github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/api_add_user_gpg_key.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 "fmt" 20 "net/http" 21 "regexp" 22 "strings" 23 24 "github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" 25 "github.com/greenpau/go-authcrunch/pkg/ids" 26 "github.com/greenpau/go-authcrunch/pkg/requests" 27 "github.com/greenpau/go-authcrunch/pkg/tagging" 28 "github.com/greenpau/go-authcrunch/pkg/user" 29 ) 30 31 var gpgKeyTitleRegexPattern = regexp.MustCompile(`^[\w\@\.\s\(\)<>,\-+:]+$`) 32 33 // AddUserGPGKey adds GPG key to user identity. 34 func (p *Portal) AddUserGPGKey( 35 ctx context.Context, 36 w http.ResponseWriter, 37 r *http.Request, 38 rr *requests.Request, 39 parsedUser *user.User, 40 resp map[string]interface{}, 41 usr *user.User, 42 backend ids.IdentityStore, 43 bodyData map[string]interface{}) error { 44 45 var keyTitle, keyDescription, keyPayload string 46 var keyLabels []string = []string{} 47 var keyTags []tagging.Tag = []tagging.Tag{} 48 49 // Extract data. 50 if v, exists := bodyData["content"]; exists { 51 switch exp := v.(type) { 52 case string: 53 keyPayload = strings.TrimSpace(exp) 54 default: 55 resp["message"] = "Profile API did find key content in the request payload, but it is malformed" 56 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 57 } 58 } else { 59 resp["message"] = "Profile API did not find key content in the request payload" 60 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 61 } 62 if v, exists := bodyData["title"]; exists { 63 keyTitle = v.(string) 64 } else { 65 resp["message"] = "Profile API did not find title in the request payload" 66 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 67 } 68 if v, exists := bodyData["description"]; exists { 69 keyDescription = v.(string) 70 } else { 71 resp["message"] = "Profile API did not find description in the request payload" 72 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 73 } 74 if extractedTokenTags, err := tagging.ExtractTags(bodyData); err == nil { 75 for _, extractedTokenTag := range extractedTokenTags { 76 keyTags = append(keyTags, *extractedTokenTag) 77 } 78 } else { 79 resp["message"] = "Profile API find malformed tags in the request payload" 80 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 81 } 82 if extractedTokenLabels, err := tagging.ExtractLabels(bodyData); err == nil { 83 keyLabels = extractedTokenLabels 84 } else { 85 resp["message"] = "Profile API find malformed tags in the request payload" 86 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 87 } 88 89 // Validate data. 90 if !gpgKeyTitleRegexPattern.MatchString(keyTitle) { 91 resp["message"] = "Profile API found non-compliant GPG key title value" 92 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 93 } 94 if !tokenDescriptionRegexPattern.MatchString(keyDescription) && (keyDescription != "") { 95 resp["message"] = "Profile API found non-compliant GPG key description value" 96 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 97 } 98 if !gpgKeyRegexPattern1.MatchString(keyPayload) && !gpgKeyRegexPattern2.MatchString(keyPayload) { 99 resp["message"] = "Profile API found non-compliant GPG public key value" 100 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 101 } 102 103 rr.Key.Usage = "gpg" 104 rr.Key.Comment = keyTitle 105 rr.Key.Description = keyDescription 106 rr.Key.Payload = keyPayload 107 rr.Key.Labels = keyLabels 108 rr.Key.Tags = keyTags 109 110 if err := backend.Request(operator.AddKeyGPG, rr); err != nil { 111 var errMsg string = fmt.Sprintf("the Profile API failed to add GPG key to identity store: %v", err) 112 resp["message"] = errMsg 113 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 114 } 115 116 resp["entry"] = "Created" 117 return handleAPIProfileResponse(w, rr, http.StatusOK, resp) 118 }