Newer
Older
package revocation
import (
"encoding/json"
"github.com/oidc-mytoken/server/internal/config"
"github.com/oidc-mytoken/server/internal/db"
"github.com/oidc-mytoken/server/internal/db/dbrepo/mytokenrepo/transfercoderepo"
"github.com/oidc-mytoken/server/internal/model"
"github.com/oidc-mytoken/server/pkg/api/v0"
sharedModel "github.com/oidc-mytoken/server/shared/model"
"github.com/oidc-mytoken/server/shared/mytoken"
mytokenPkg "github.com/oidc-mytoken/server/shared/mytoken/pkg"
"github.com/oidc-mytoken/server/shared/mytoken/token"
// HandleRevoke handles requests to the revocation endpoint
func HandleRevoke(ctx *fiber.Ctx) error {
if err := json.Unmarshal(ctx.Body(), &req); err != nil {
return model.ErrorToBadRequestErrorResponse(err).Send(ctx)
}
clearCookie := false
clearCookie = true
}
errRes := revokeAnyToken(nil, req.Token, req.OIDCIssuer, req.Recursive)
if errRes != nil {
return errRes.Send(ctx)
}
if clearCookie {
return model.Response{
Status: fiber.StatusNoContent,
Cookies: []*fiber.Cookie{{
Value: "",
Path: "/api",
Expires: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
Secure: config.Get().Server.Secure,
HTTPOnly: true,
SameSite: "Strict",
}},
}.Send(ctx)
}
return ctx.SendStatus(fiber.StatusNoContent)
}
func revokeAnyToken(tx *sqlx.Tx, token, issuer string, recursive bool) (errRes *model.Response) {
if utils.IsJWT(token) { // normal Mytoken
return revokeMytoken(tx, token, issuer, recursive)
} else if len(token) == config.Get().Features.Polling.Len { // Transfer Code
return revokeTransferCode(tx, token, issuer)
shortToken := transfercoderepo.ParseShortToken(token)
var valid bool
if err := db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error {
jwt, v, err := shortToken.JWT(tx)
valid = v
if err != nil {
return err
}
token = jwt
}); err != nil {
return model.ErrorToInternalServerErrorResponse(err)
return nil
return revokeMytoken(tx, token, issuer, recursive)
func revokeMytoken(tx *sqlx.Tx, jwt, issuer string, recursive bool) (errRes *model.Response) {
mt, err := mytokenPkg.ParseJWT(jwt)
if issuer != "" && mt.OIDCIssuer != issuer {
Response: sharedModel.BadRequestError("token not for specified issuer"),
return mytoken.RevokeMytoken(tx, mt.ID, token.Token(jwt), recursive, mt.OIDCIssuer)
func revokeTransferCode(tx *sqlx.Tx, token, issuer string) (errRes *model.Response) {
transferCode := transfercoderepo.ParseTransferCode(token)
err := db.RunWithinTransaction(tx, func(tx *sqlx.Tx) error {
revokeMT, err := transferCode.GetRevokeJWT(tx)
if err != nil {
return err
}
jwt, valid, err := transferCode.JWT(tx)
if err != nil {
return err
}
if valid { // if !valid the jwt field could not decrypted correctly, so we can skip that, but still delete the TransferCode
errRes = revokeAnyToken(tx, jwt, issuer, true)
if errRes != nil {
return fmt.Errorf("placeholder")
}
}
}
return transferCode.Delete(tx)
})
return model.ErrorToInternalServerErrorResponse(err)