This commit is contained in:
@@ -5,19 +5,19 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
auth_model "code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
"code.gitea.io/gitea/services/convert"
|
||||
auth_model "gitea.dev/models/auth"
|
||||
"gitea.dev/models/db"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/web"
|
||||
"gitea.dev/routers/api/v1/utils"
|
||||
"gitea.dev/services/context"
|
||||
"gitea.dev/services/convert"
|
||||
"gitea.dev/services/forms"
|
||||
)
|
||||
|
||||
// ListAccessTokens list all the access tokens
|
||||
@@ -111,13 +111,13 @@ func CreateAccessToken(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
ctx.APIError(http.StatusBadRequest, errors.New("access token name has been used already"))
|
||||
ctx.APIError(http.StatusBadRequest, "access token name has been used already")
|
||||
return
|
||||
}
|
||||
|
||||
scope, err := auth_model.AccessTokenScope(strings.Join(form.Scopes, ",")).Normalize()
|
||||
if err != nil {
|
||||
ctx.APIError(http.StatusBadRequest, fmt.Errorf("invalid access token scope provided: %w", err))
|
||||
ctx.APIError(http.StatusBadRequest, fmt.Sprintf("invalid access token scope provided: %v", err))
|
||||
return
|
||||
}
|
||||
if scope == "" {
|
||||
@@ -126,6 +126,29 @@ func CreateAccessToken(ctx *context.APIContext) {
|
||||
}
|
||||
t.Scope = scope
|
||||
|
||||
// a token-authenticated request must not mint a token with a broader scope than its own
|
||||
if ctx.Data["IsApiToken"] == true {
|
||||
apiTokenScope, ok := ctx.Data["ApiTokenScope"].(auth_model.AccessTokenScope)
|
||||
if !ok {
|
||||
ctx.APIError(http.StatusForbidden, "the authenticating token has no scope")
|
||||
return
|
||||
}
|
||||
hasScope, err := apiTokenScope.CanCreateChildScope(scope)
|
||||
if err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
if !hasScope {
|
||||
ctx.APIError(http.StatusForbidden, "cannot create an access token with a broader scope than the authenticating token")
|
||||
return
|
||||
}
|
||||
// a public-only token must not mint a token that drops the public-only restriction
|
||||
if t.Scope, err = t.Scope.EnforcePublicOnlyFrom(apiTokenScope); err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := auth_model.NewAccessToken(ctx, t); err != nil {
|
||||
ctx.APIErrorInternal(err)
|
||||
return
|
||||
@@ -187,21 +210,13 @@ func DeleteAccessToken(ctx *context.APIContext) {
|
||||
case 1:
|
||||
tokenID = tokens[0].ID
|
||||
default:
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Errorf("multiple matches for token name '%s'", token))
|
||||
ctx.APIError(http.StatusUnprocessableEntity, fmt.Sprintf("multiple matches for token name '%s'", token))
|
||||
return
|
||||
}
|
||||
}
|
||||
if tokenID == 0 {
|
||||
ctx.APIErrorInternal(nil)
|
||||
return
|
||||
}
|
||||
|
||||
if err := auth_model.DeleteAccessTokenByID(ctx, tokenID, ctx.ContextUser.ID); err != nil {
|
||||
if auth_model.IsErrAccessTokenNotExist(err) {
|
||||
ctx.APIErrorNotFound()
|
||||
} else {
|
||||
ctx.APIErrorInternal(err)
|
||||
}
|
||||
ctx.APIErrorAuto(err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -228,7 +243,10 @@ func CreateOauth2Application(ctx *context.APIContext) {
|
||||
// "$ref": "#/responses/error"
|
||||
|
||||
data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
|
||||
|
||||
if invalidURI := forms.DetectInvalidOAuth2ApplicationRedirectURI(data.RedirectURIs); invalidURI != "" {
|
||||
ctx.APIError(http.StatusBadRequest, "invalid redirect URI: "+invalidURI)
|
||||
return
|
||||
}
|
||||
app, err := auth_model.CreateOAuth2Application(ctx, auth_model.CreateOAuth2ApplicationOptions{
|
||||
Name: data.Name,
|
||||
UserID: ctx.Doer.ID,
|
||||
@@ -382,11 +400,17 @@ func UpdateOauth2Application(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/OAuth2Application"
|
||||
// "400":
|
||||
// "$ref": "#/responses/error"
|
||||
// "404":
|
||||
// "$ref": "#/responses/notFound"
|
||||
appID := ctx.PathParamInt64("id")
|
||||
|
||||
data := web.GetForm(ctx).(*api.CreateOAuth2ApplicationOptions)
|
||||
if invalidURI := forms.DetectInvalidOAuth2ApplicationRedirectURI(data.RedirectURIs); invalidURI != "" {
|
||||
ctx.APIError(http.StatusBadRequest, "invalid redirect URI: "+invalidURI)
|
||||
return
|
||||
}
|
||||
|
||||
app, err := auth_model.UpdateOAuth2Application(ctx, auth_model.UpdateOAuth2ApplicationOptions{
|
||||
Name: data.Name,
|
||||
|
||||
Reference in New Issue
Block a user