github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/api_test_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/identity" 25 "github.com/greenpau/go-authcrunch/pkg/ids" 26 "github.com/greenpau/go-authcrunch/pkg/requests" 27 "github.com/greenpau/go-authcrunch/pkg/user" 28 ) 29 30 var gpgKeyRegexPattern1 = regexp.MustCompile(`^[-]{3,5}\s*BEGIN\sPGP\sPUBLIC\sKEY\sBLOCK[-]{3,5}\s*`) 31 var gpgKeyRegexPattern2 = regexp.MustCompile(`[-]{3,5}\s*END\sPGP\sPUBLIC\sKEY\sBLOCK[-]{3,5}\s*$`) 32 33 // TestUserGPGKey tests GPG key. 34 func (p *Portal) TestUserGPGKey( 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 rr.Key.Usage = "gpg" 46 47 // Extract data. 48 if v, exists := bodyData["content"]; exists { 49 switch keyContent := v.(type) { 50 case string: 51 rr.Key.Payload = strings.TrimSpace(keyContent) 52 default: 53 resp["message"] = "Profile API did find key content in the request payload, but it is malformed" 54 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 55 } 56 } else { 57 resp["message"] = "Profile API did not find key content in the request payload" 58 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 59 } 60 61 // Validate data. 62 if !gpgKeyRegexPattern1.MatchString(rr.Key.Payload) || !gpgKeyRegexPattern2.MatchString(rr.Key.Payload) { 63 resp["message"] = "Profile API found non-compliant GPG public key value" 64 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 65 } 66 67 pk, err := identity.NewPublicKey(rr) 68 if err != nil { 69 var errMsg string = fmt.Sprintf("the Profile API failed to convert input into GPG key: %v", err) 70 resp["message"] = errMsg 71 return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) 72 } 73 74 respData := make(map[string]interface{}) 75 if pk != nil { 76 respData["success"] = true 77 if pk.FingerprintMD5 != "" { 78 respData["fingerprint_md5"] = pk.FingerprintMD5 79 } 80 if pk.Fingerprint != "" { 81 respData["fingerprint"] = pk.Fingerprint 82 } 83 if pk.Comment != "" { 84 respData["comment"] = pk.Comment 85 } 86 } else { 87 respData["success"] = false 88 } 89 resp["entry"] = respData 90 return handleAPIProfileResponse(w, rr, http.StatusOK, resp) 91 }