MCPcopy Index your code
hub / github.com/coder/coder / regenerateGitSSHKey

Method regenerateGitSSHKey

coderd/gitsshkey.go:24–78  ·  view source on GitHub ↗

@Summary Regenerate user SSH key @ID regenerate-user-ssh-key @Security CoderSessionToken @Produce json @Tags Users @Param user path string true "User ID, name, or me" @Success 200 {object} codersdk.GitSSHKey @Router /api/v2/users/{user}/gitsshkey [put]

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

22// @Success 200 {object} codersdk.GitSSHKey
23// @Router /api/v2/users/{user}/gitsshkey [put]
24func (api *API) regenerateGitSSHKey(rw http.ResponseWriter, r *http.Request) {
25 var (
26 ctx = r.Context()
27 user = httpmw.UserParam(r)
28 auditor = api.Auditor.Load()
29 aReq, commitAudit = audit.InitRequest[database.GitSSHKey](rw, &audit.RequestParams{
30 Audit: *auditor,
31 Log: api.Logger,
32 Request: r,
33 Action: database.AuditActionWrite,
34 })
35 )
36 defer commitAudit()
37
38 oldKey, err := api.Database.GetGitSSHKey(ctx, user.ID)
39 if err != nil {
40 httpapi.InternalServerError(rw, err)
41 return
42 }
43
44 aReq.Old = oldKey
45
46 privateKey, publicKey, err := gitsshkey.Generate(api.SSHKeygenAlgorithm)
47 if err != nil {
48 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
49 Message: "Internal error generating a new SSH keypair.",
50 Detail: err.Error(),
51 })
52 return
53 }
54
55 newKey, err := api.Database.UpdateGitSSHKey(ctx, database.UpdateGitSSHKeyParams{
56 UserID: user.ID,
57 UpdatedAt: dbtime.Now(),
58 PrivateKey: privateKey,
59 PublicKey: publicKey,
60 })
61 if err != nil {
62 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
63 Message: "Internal error updating user's git SSH key.",
64 Detail: err.Error(),
65 })
66 return
67 }
68
69 aReq.New = newKey
70
71 httpapi.Write(ctx, rw, http.StatusOK, codersdk.GitSSHKey{
72 UserID: newKey.UserID,
73 CreatedAt: newKey.CreatedAt,
74 UpdatedAt: newKey.UpdatedAt,
75 // No need to return the private key to the user
76 PublicKey: newKey.PublicKey,
77 })
78}
79
80// @Summary Get user Git SSH key
81// @ID get-user-git-ssh-key

Callers

nothing calls this directly

Calls 11

UserParamFunction · 0.92
InitRequestFunction · 0.92
InternalServerErrorFunction · 0.92
GenerateFunction · 0.92
WriteFunction · 0.92
NowFunction · 0.92
ContextMethod · 0.65
GetGitSSHKeyMethod · 0.65
UpdateGitSSHKeyMethod · 0.65
LoadMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected