corso/src/pkg/account/m365.go
Keepers 689c5cc1e9
separate tenantID from m365 creds (#231)
* separate tenantID from m365 creds

Now that account.Account is in place, tenant id needs
to get removed from the credential set (it isn't actually
a secret) and placed in the account configuration instead.
2022-06-27 13:54:37 -06:00

62 lines
1.4 KiB
Go

package account
import (
"github.com/pkg/errors"
"github.com/alcionai/corso/pkg/credentials"
)
// config exported name consts
const (
TenantID = "TENANT_ID"
)
type M365Config struct {
credentials.M365 // requires: ClientID, ClientSecret
TenantID string
}
// config key consts
const (
keyM365ClientID = "m365_clientID"
keyM365ClientSecret = "m365_clientSecret"
keyM365TenantID = "m365_tenantID"
)
// StringConfig transforms a m365Config struct into a plain
// map[string]string. All values in the original struct which
// serialize into the map are expected to be strings.
func (c M365Config) StringConfig() (map[string]string, error) {
cfg := map[string]string{
keyM365ClientID: c.ClientID,
keyM365ClientSecret: c.ClientSecret,
keyM365TenantID: c.TenantID,
}
return cfg, c.validate()
}
// M365Config retrieves the M365Config details from the Account config.
func (a Account) M365Config() (M365Config, error) {
c := M365Config{}
if len(a.Config) > 0 {
c.ClientID = a.Config[keyM365ClientID]
c.ClientSecret = a.Config[keyM365ClientSecret]
c.TenantID = a.Config[keyM365TenantID]
}
return c, c.validate()
}
func (c M365Config) validate() error {
check := map[string]string{
credentials.ClientID: c.ClientID,
credentials.ClientSecret: c.ClientSecret,
TenantID: c.TenantID,
}
for k, v := range check {
if len(v) == 0 {
return errors.Wrap(errMissingRequired, k)
}
}
return nil
}