-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add ssh key add operations to profile api
- Loading branch information
Showing
10 changed files
with
271 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package authn | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" | ||
"github.com/greenpau/go-authcrunch/pkg/ids" | ||
"github.com/greenpau/go-authcrunch/pkg/requests" | ||
"github.com/greenpau/go-authcrunch/pkg/tagging" | ||
"github.com/greenpau/go-authcrunch/pkg/user" | ||
) | ||
|
||
var sshKeyTitleRegexPattern = regexp.MustCompile(`^[\w\@\.\s\(\)]+$`) | ||
|
||
// AddUserSSHKey adds SSH key to user identity. | ||
func (p *Portal) AddUserSSHKey( | ||
ctx context.Context, | ||
w http.ResponseWriter, | ||
r *http.Request, | ||
rr *requests.Request, | ||
parsedUser *user.User, | ||
resp map[string]interface{}, | ||
usr *user.User, | ||
backend ids.IdentityStore, | ||
bodyData map[string]interface{}) error { | ||
|
||
var keyTitle, keyDescription, keyPayload string | ||
var keyLabels []string = []string{} | ||
var keyTags []tagging.Tag = []tagging.Tag{} | ||
|
||
// Extract data. | ||
if v, exists := bodyData["content"]; exists { | ||
switch exp := v.(type) { | ||
case string: | ||
keyPayload = strings.TrimSpace(exp) | ||
default: | ||
resp["message"] = "Profile API did find key content in the request payload, but it is malformed" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
} else { | ||
resp["message"] = "Profile API did not find key content in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if v, exists := bodyData["title"]; exists { | ||
keyTitle = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find title in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if v, exists := bodyData["description"]; exists { | ||
keyDescription = v.(string) | ||
} else { | ||
resp["message"] = "Profile API did not find description in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if extractedTokenTags, err := tagging.ExtractTags(bodyData); err == nil { | ||
for _, extractedTokenTag := range extractedTokenTags { | ||
keyTags = append(keyTags, *extractedTokenTag) | ||
} | ||
} else { | ||
resp["message"] = "Profile API find malformed tags in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if extractedTokenLabels, err := tagging.ExtractLabels(bodyData); err == nil { | ||
keyLabels = extractedTokenLabels | ||
} else { | ||
resp["message"] = "Profile API find malformed tags in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
// Validate data. | ||
if !sshKeyTitleRegexPattern.MatchString(keyTitle) { | ||
resp["message"] = "Profile API found non-compliant SSH key title value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if !tokenDescriptionRegexPattern.MatchString(keyDescription) && (keyDescription != "") { | ||
resp["message"] = "Profile API found non-compliant SSH key description value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
if !sshKeyRegexPattern1.MatchString(keyPayload) && !sshKeyRegexPattern2.MatchString(keyPayload) { | ||
resp["message"] = "Profile API found non-compliant SSH public key value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
rr.Key.Usage = "ssh" | ||
rr.Key.Comment = keyTitle | ||
rr.Key.Description = keyDescription | ||
rr.Key.Payload = keyPayload | ||
rr.Key.Labels = keyLabels | ||
rr.Key.Tags = keyTags | ||
|
||
if err := backend.Request(operator.AddKeySSH, rr); err != nil { | ||
var errMsg string = fmt.Sprintf("the Profile API failed to add SSH key to identity store: %v", err) | ||
resp["message"] = errMsg | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
resp["entry"] = "Created" | ||
return handleAPIProfileResponse(w, rr, http.StatusOK, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 Paul Greenberg [email protected] | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package authn | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"regexp" | ||
|
||
"github.com/greenpau/go-authcrunch/pkg/identity" | ||
"github.com/greenpau/go-authcrunch/pkg/ids" | ||
"github.com/greenpau/go-authcrunch/pkg/requests" | ||
"github.com/greenpau/go-authcrunch/pkg/user" | ||
) | ||
|
||
var sshKeyRegexPattern1 = regexp.MustCompile(`^ssh-[a-z]+\s*[A-z0-9\+\/\=\n]+\s*`) | ||
var sshKeyRegexPattern2 = regexp.MustCompile(`^[-]{3,5}\s*BEGIN\s[A-Z0-9]+\sPUBLIC\sKEY[-]{3,5}\s*`) | ||
|
||
// TestUserSSHKey tests SSH key. | ||
func (p *Portal) TestUserSSHKey( | ||
ctx context.Context, | ||
w http.ResponseWriter, | ||
r *http.Request, | ||
rr *requests.Request, | ||
parsedUser *user.User, | ||
resp map[string]interface{}, | ||
usr *user.User, | ||
backend ids.IdentityStore, | ||
bodyData map[string]interface{}) error { | ||
|
||
rr.Key.Usage = "ssh" | ||
|
||
// Extract data. | ||
if v, exists := bodyData["content"]; exists { | ||
switch keyContent := v.(type) { | ||
case string: | ||
rr.Key.Payload = keyContent | ||
default: | ||
resp["message"] = "Profile API did find key content in the request payload, but it is malformed" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
} else { | ||
resp["message"] = "Profile API did not find key content in the request payload" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
// Validate data. | ||
if !sshKeyRegexPattern1.MatchString(rr.Key.Payload) && !sshKeyRegexPattern2.MatchString(rr.Key.Payload) { | ||
resp["message"] = "Profile API found non-compliant SSH public key value" | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
pk, err := identity.NewPublicKey(rr) | ||
if err != nil { | ||
var errMsg string = fmt.Sprintf("the Profile API failed to convert input into SSH key: %v", err) | ||
resp["message"] = errMsg | ||
return handleAPIProfileResponse(w, rr, http.StatusBadRequest, resp) | ||
} | ||
|
||
respData := make(map[string]interface{}) | ||
if pk != nil { | ||
respData["success"] = true | ||
if pk.FingerprintMD5 != "" { | ||
respData["fingerprint_md5"] = pk.FingerprintMD5 | ||
} | ||
if pk.Fingerprint != "" { | ||
respData["fingerprint"] = pk.Fingerprint | ||
} | ||
if pk.Comment != "" { | ||
respData["comment"] = pk.Comment | ||
} | ||
} else { | ||
respData["success"] = false | ||
} | ||
resp["entry"] = respData | ||
return handleAPIProfileResponse(w, rr, http.StatusOK, resp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.