Files
pocket-id/backend/internal/model/oidc.go

154 lines
3.4 KiB
Go
Raw Normal View History

2024-08-12 11:00:25 +02:00
package model
import (
2024-08-23 17:04:19 +02:00
"database/sql/driver"
"encoding/json"
"strings"
datatype "github.com/pocket-id/pocket-id/backend/internal/model/types"
2025-10-24 12:14:19 +02:00
"github.com/pocket-id/pocket-id/backend/internal/utils"
2024-08-12 11:00:25 +02:00
)
type UserAuthorizedOidcClient struct {
Scope string
LastUsedAt datatype.DateTime `sortable:"true"`
2024-08-23 17:04:19 +02:00
UserID string `gorm:"primary_key;"`
User User
2024-08-12 11:00:25 +02:00
2024-08-23 17:04:19 +02:00
ClientID string `gorm:"primary_key;"`
2024-08-12 11:00:25 +02:00
Client OidcClient
}
func (c UserAuthorizedOidcClient) Scopes() []string {
if len(c.Scope) == 0 {
return []string{}
}
return strings.Split(c.Scope, " ")
}
2024-08-12 11:00:25 +02:00
type OidcAuthorizationCode struct {
Base
2024-11-15 15:00:25 +01:00
Code string
Scope string
Nonce string
CodeChallenge *string
CodeChallengeMethodSha256 *bool
ExpiresAt datatype.DateTime
2024-08-12 11:00:25 +02:00
UserID string
User User
ClientID string
}
2024-08-23 17:04:19 +02:00
type OidcClient struct {
Base
Name string `sortable:"true"`
Secret string
CallbackURLs UrlList
LogoutCallbackURLs UrlList
ImageType *string
DarkImageType *string
IsPublic bool
PkceEnabled bool `sortable:"true" filterable:"true"`
RequiresReauthentication bool `sortable:"true" filterable:"true"`
Credentials OidcClientCredentials
LaunchURL *string
2024-08-23 17:04:19 +02:00
AllowedUserGroups []UserGroup `gorm:"many2many:oidc_clients_allowed_user_groups;"`
CreatedByID *string
CreatedBy *User
UserAuthorizedOidcClients []UserAuthorizedOidcClient `gorm:"foreignKey:ClientID;references:ID"`
2024-08-12 11:00:25 +02:00
}
func (c OidcClient) HasLogo() bool {
return c.ImageType != nil && *c.ImageType != ""
}
func (c OidcClient) HasDarkLogo() bool {
return c.DarkImageType != nil && *c.DarkImageType != ""
}
type OidcRefreshToken struct {
Base
Token string
ExpiresAt datatype.DateTime
Scope string
UserID string
User User
ClientID string
Client OidcClient
}
func (c OidcRefreshToken) Scopes() []string {
if len(c.Scope) == 0 {
return []string{}
}
return strings.Split(c.Scope, " ")
}
type OidcClientCredentials struct { //nolint:recvcheck
FederatedIdentities []OidcClientFederatedIdentity `json:"federatedIdentities,omitempty"`
}
type OidcClientFederatedIdentity struct {
Issuer string `json:"issuer"`
Subject string `json:"subject,omitempty"`
Audience string `json:"audience,omitempty"`
JWKS string `json:"jwks,omitempty"` // URL of the JWKS
}
func (occ OidcClientCredentials) FederatedIdentityForIssuer(issuer string) (OidcClientFederatedIdentity, bool) {
if issuer == "" {
return OidcClientFederatedIdentity{}, false
}
for _, fi := range occ.FederatedIdentities {
if fi.Issuer == issuer {
return fi, true
}
}
return OidcClientFederatedIdentity{}, false
}
func (occ *OidcClientCredentials) Scan(value any) error {
2025-10-24 12:14:19 +02:00
return utils.UnmarshalJSONFromDatabase(occ, value)
}
func (occ OidcClientCredentials) Value() (driver.Value, error) {
return json.Marshal(occ)
}
2025-03-27 16:48:36 +01:00
type UrlList []string //nolint:recvcheck
2024-08-23 17:04:19 +02:00
func (cu *UrlList) Scan(value any) error {
2025-10-24 12:14:19 +02:00
return utils.UnmarshalJSONFromDatabase(cu, value)
2024-08-12 11:00:25 +02:00
}
2025-02-14 17:09:27 +01:00
func (cu UrlList) Value() (driver.Value, error) {
return json.Marshal(cu)
}
type OidcDeviceCode struct {
Base
DeviceCode string
UserCode string
Scope string
ExpiresAt datatype.DateTime
IsAuthorized bool
UserID *string
User User
ClientID string
Client OidcClient
}