grafana/pkg/services/ssosettings/models/models.go
colin-stuart 062e772bb2
Auth: Implement the SSO Settings GET endpoint (#79144)
* Return data in camelCase from the OAuth fb strategy

* changes

* wip

* Add defaults for oauth fb strategy

* revert other changes

* basic includeDefaults query param implementation

* basic secret removal and etag implementation

* correct imports

* rebase

* move default settings filter to models

* only replace ClientSecret value if set

* first GetForProvider test & use FNV for ETag to avoid Blocklisted import error

* add tests

* add annotation for the openapi spec & generate spec

* remove TODO

* use IsSecret, improve tests, remove DefaultOAuthSettings

* add comment explaining generateFNVETag

* add error handling for generateFNVETag

* run go generate

* Update pkg/services/ssosettings/api/api.go

Co-authored-by: Mihai Doarna <mihai.doarna@grafana.com>

* move isSecret to service, create GetForProviderWithRedactedSecrets func

* add unit test for GetForProviderWithRedactedSecrets & remove duplicated code

* regen openapi/swagger

* revert dependency bumps

---------

Co-authored-by: Mihaly Gyongyosi <mgyongyosi@users.noreply.github.com>
Co-authored-by: Mihai Doarna <mihai.doarna@grafana.com>
2024-01-08 09:35:14 -05:00

100 lines
2.1 KiB
Go

package models
import (
"encoding/json"
"fmt"
"time"
"github.com/grafana/grafana/pkg/services/featuremgmt/strcase"
)
type SettingsSource int
const (
DB = iota
System
)
func (s SettingsSource) MarshalJSON() ([]byte, error) {
switch s {
case DB:
return json.Marshal("database")
case System:
return json.Marshal("system")
default:
return nil, fmt.Errorf("unknown source: %d", s)
}
}
func (s *SettingsSource) UnmarshalJSON(data []byte) error {
var source string
if err := json.Unmarshal(data, &source); err != nil {
return err
}
switch source {
case "database":
*s = DB
case "system":
*s = System
default:
return fmt.Errorf("unknown source: %s", source)
}
return nil
}
type SSOSettings struct {
ID string `xorm:"id pk" json:"id"`
Provider string `xorm:"provider" json:"provider"`
Settings map[string]any `xorm:"settings" json:"settings"`
Created time.Time `xorm:"created" json:"-"`
Updated time.Time `xorm:"updated" json:"-"`
IsDeleted bool `xorm:"is_deleted" json:"-"`
Source SettingsSource `xorm:"-" json:"source"`
}
// TableName returns the table name (needed for Xorm)
func (s SSOSettings) TableName() string {
return "sso_setting"
}
// MarshalJSON implements the json.Marshaler interface and converts the s.Settings from map[string]any in snake_case to map[string]any in camelCase
func (s SSOSettings) MarshalJSON() ([]byte, error) {
type Alias SSOSettings
aux := &struct {
*Alias
}{
Alias: (*Alias)(&s),
}
settings := make(map[string]any)
for k, v := range aux.Settings {
settings[strcase.ToLowerCamel(k)] = v
}
aux.Settings = settings
return json.Marshal(aux)
}
// UnmarshalJSON implements the json.Unmarshaler interface and converts the settings from map[string]any camelCase to map[string]interface{} snake_case
func (s *SSOSettings) UnmarshalJSON(data []byte) error {
type Alias SSOSettings
aux := &struct {
*Alias
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
settings := make(map[string]any)
for k, v := range aux.Settings {
settings[strcase.ToSnake(k)] = v
}
s.Settings = settings
return nil
}