// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.

package oauth

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

type RevokeRequest struct {
	Token string `json:"token" src:"form" desc:"要撤销的令牌"`
}

func revoke(x *vigo.X, req *RevokeRequest) error {
	// 查找并撤销令牌
	var token models.OAuthToken
	if err := cfg.DB().First(&token, "access_token = ? OR refresh_token = ?", req.Token, req.Token).Error; err != nil {
		return nil // 按照OAuth规范，不存在的令牌也返回成功
	}

	cfg.DB().Model(&token).Update("revoked", true)
	return nil
}
