mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package token
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
auth_model "github.com/hanzoai/git/models/auth"
|
|
user_model "github.com/hanzoai/git/models/user"
|
|
"github.com/hanzoai/git/modules/auth/httpauth"
|
|
api "github.com/hanzoai/git/modules/structs"
|
|
"github.com/hanzoai/git/modules/util"
|
|
"github.com/hanzoai/git/services/context"
|
|
)
|
|
|
|
// GetCurrentToken returns metadata about the currently authenticated token.
|
|
func GetCurrentToken(ctx *context.APIContext) {
|
|
// swagger:operation GET /token miscellaneous getCurrentToken
|
|
// ---
|
|
// summary: Get the currently authenticated token
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/CurrentAccessToken"
|
|
accessToken, err := getToken(ctx)
|
|
if err != nil {
|
|
ctx.APIErrorAuto(err)
|
|
return
|
|
}
|
|
|
|
// Get user info
|
|
user, err := user_model.GetUserByID(ctx, accessToken.UID)
|
|
if err != nil {
|
|
ctx.APIErrorAuto(err)
|
|
return
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, &api.CurrentAccessToken{
|
|
ID: accessToken.ID,
|
|
Name: accessToken.Name,
|
|
Scopes: accessToken.Scope.StringSlice(),
|
|
CreatedAt: accessToken.CreatedUnix.AsTime(),
|
|
LastUsedAt: accessToken.UpdatedUnix.AsTime(),
|
|
User: &api.UserMeta{
|
|
ID: user.ID,
|
|
Login: user.Name,
|
|
},
|
|
})
|
|
}
|
|
|
|
// DeleteCurrentToken deletes the currently authenticated token.
|
|
func DeleteCurrentToken(ctx *context.APIContext) {
|
|
// swagger:operation DELETE /token miscellaneous deleteCurrentToken
|
|
// ---
|
|
// summary: Delete the currently authenticated token
|
|
// produces:
|
|
// - application/json
|
|
// responses:
|
|
// "204":
|
|
// description: token deleted
|
|
accessToken, err := getToken(ctx)
|
|
if err != nil {
|
|
ctx.APIErrorAuto(err)
|
|
return
|
|
}
|
|
|
|
// Delete the token
|
|
err = auth_model.DeleteAccessTokenByID(ctx, accessToken.ID, accessToken.UID)
|
|
if err != nil && !errors.Is(err, util.ErrNotExist) {
|
|
ctx.APIErrorAuto(err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
// getToken retrieves an access token from the API context's Authorization header and validates it against the database.
|
|
// Returns nil if the token is invalid and handles the response
|
|
func getToken(ctx *context.APIContext) (*auth_model.AccessToken, error) {
|
|
authHeader := ctx.Req.Header.Get("Authorization")
|
|
parsed, ok := httpauth.ParseAuthorizationHeader(authHeader)
|
|
if !ok || parsed.BearerToken == nil {
|
|
return nil, util.NewNotExistErrorf("invalid access token")
|
|
}
|
|
return auth_model.GetAccessTokenBySHA(ctx, parsed.BearerToken.Token)
|
|
}
|