2023-03-08 23:30:39 +01:00
|
|
|
package authn
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
import (
|
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
|
|
|
"github.com/photoprism/photoprism/pkg/list"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProviderType represents an authentication provider type.
|
|
|
|
type ProviderType string
|
|
|
|
|
2023-03-08 23:30:39 +01:00
|
|
|
// Authentication providers.
|
|
|
|
const (
|
2023-03-09 15:12:10 +01:00
|
|
|
ProviderDefault ProviderType = "default"
|
|
|
|
ProviderLocal ProviderType = "local"
|
|
|
|
ProviderLDAP ProviderType = "ldap"
|
2023-03-13 16:04:37 +01:00
|
|
|
ProviderLink ProviderType = "link"
|
2023-03-09 15:12:10 +01:00
|
|
|
ProviderNone ProviderType = "none"
|
|
|
|
ProviderUnknown ProviderType = ""
|
2023-03-08 23:30:39 +01:00
|
|
|
)
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// RemoteProviders lists all remote auth providers.
|
|
|
|
var RemoteProviders = list.List{
|
|
|
|
string(ProviderLDAP),
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalProviders lists all local auth providers.
|
|
|
|
var LocalProviders = list.List{
|
|
|
|
string(ProviderLocal),
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRemote checks if the provider is external.
|
|
|
|
func (t ProviderType) IsRemote() bool {
|
|
|
|
return list.Contains(RemoteProviders, string(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLocal checks if local authentication is possible.
|
|
|
|
func (t ProviderType) IsLocal() bool {
|
|
|
|
return list.Contains(LocalProviders, string(t))
|
|
|
|
}
|
|
|
|
|
2023-03-13 16:04:37 +01:00
|
|
|
// IsDefault checks if this is the default provider.
|
|
|
|
func (t ProviderType) IsDefault() bool {
|
|
|
|
return t.String() == ProviderDefault.String()
|
|
|
|
}
|
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// String returns the provider identifier as a string.
|
|
|
|
func (t ProviderType) String() string {
|
2023-03-09 15:59:08 +01:00
|
|
|
switch t {
|
|
|
|
case "":
|
2023-03-09 15:12:10 +01:00
|
|
|
return string(ProviderDefault)
|
2023-03-13 16:04:37 +01:00
|
|
|
case "token":
|
|
|
|
return string(ProviderLink)
|
2023-03-09 15:59:08 +01:00
|
|
|
case "password":
|
2023-03-09 15:12:10 +01:00
|
|
|
return string(ProviderLocal)
|
2023-03-09 15:59:08 +01:00
|
|
|
default:
|
|
|
|
return string(t)
|
2023-03-09 15:12:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pretty returns the provider identifier in an easy-to-read format.
|
|
|
|
func (t ProviderType) Pretty() string {
|
|
|
|
switch t {
|
|
|
|
case ProviderLDAP:
|
|
|
|
return "LDAP/AD"
|
|
|
|
default:
|
|
|
|
return txt.UpperFirst(t.String())
|
2023-03-08 23:30:39 +01:00
|
|
|
}
|
2023-03-09 15:12:10 +01:00
|
|
|
}
|
2023-03-08 23:30:39 +01:00
|
|
|
|
2023-03-09 15:12:10 +01:00
|
|
|
// Provider casts a string to a normalized provider type.
|
|
|
|
func Provider(s string) ProviderType {
|
|
|
|
switch s {
|
|
|
|
case "", "-", "null", "nil", "0", "false":
|
|
|
|
return ProviderDefault
|
2023-03-13 16:04:37 +01:00
|
|
|
case "token", "url":
|
|
|
|
return ProviderLink
|
2023-03-09 15:12:10 +01:00
|
|
|
case "pass", "passwd", "password":
|
|
|
|
return ProviderLocal
|
|
|
|
case "ldap", "ad", "ldap/ad", "ldap\\ad":
|
|
|
|
return ProviderLDAP
|
|
|
|
default:
|
|
|
|
return ProviderType(clean.TypeLower(s))
|
|
|
|
}
|
2023-03-08 23:30:39 +01:00
|
|
|
}
|