code.gitea.io/gitea@v1.21.7/routers/private/key.go (about)

     1  // Copyright 2018 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package private
     5  
     6  import (
     7  	"net/http"
     8  
     9  	asymkey_model "code.gitea.io/gitea/models/asymkey"
    10  	"code.gitea.io/gitea/modules/context"
    11  	"code.gitea.io/gitea/modules/private"
    12  	"code.gitea.io/gitea/modules/timeutil"
    13  )
    14  
    15  // UpdatePublicKeyInRepo update public key and deploy key updates
    16  func UpdatePublicKeyInRepo(ctx *context.PrivateContext) {
    17  	keyID := ctx.ParamsInt64(":id")
    18  	repoID := ctx.ParamsInt64(":repoid")
    19  	if err := asymkey_model.UpdatePublicKeyUpdated(keyID); err != nil {
    20  		ctx.JSON(http.StatusInternalServerError, private.Response{
    21  			Err: err.Error(),
    22  		})
    23  		return
    24  	}
    25  
    26  	deployKey, err := asymkey_model.GetDeployKeyByRepo(ctx, keyID, repoID)
    27  	if err != nil {
    28  		if asymkey_model.IsErrDeployKeyNotExist(err) {
    29  			ctx.PlainText(http.StatusOK, "success")
    30  			return
    31  		}
    32  		ctx.JSON(http.StatusInternalServerError, private.Response{
    33  			Err: err.Error(),
    34  		})
    35  		return
    36  	}
    37  	deployKey.UpdatedUnix = timeutil.TimeStampNow()
    38  	if err = asymkey_model.UpdateDeployKeyCols(deployKey, "updated_unix"); err != nil {
    39  		ctx.JSON(http.StatusInternalServerError, private.Response{
    40  			Err: err.Error(),
    41  		})
    42  		return
    43  	}
    44  
    45  	ctx.PlainText(http.StatusOK, "success")
    46  }
    47  
    48  // AuthorizedPublicKeyByContent searches content as prefix (leak e-mail part)
    49  // and returns public key found.
    50  func AuthorizedPublicKeyByContent(ctx *context.PrivateContext) {
    51  	content := ctx.FormString("content")
    52  
    53  	publicKey, err := asymkey_model.SearchPublicKeyByContent(ctx, content)
    54  	if err != nil {
    55  		ctx.JSON(http.StatusInternalServerError, private.Response{
    56  			Err: err.Error(),
    57  		})
    58  		return
    59  	}
    60  	ctx.PlainText(http.StatusOK, publicKey.AuthorizedString())
    61  }