photoprism/internal/entity/auth_session_client.go
Michael Mayer a4e2bb33b9 2FA: Rename "Auth Secret" to "App Password" for more clarity #782 #808
Signed-off-by: Michael Mayer <michael@photoprism.app>
2024-01-19 18:10:01 +01:00

41 lines
1.1 KiB
Go

package entity
import (
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/rnd"
)
// NewClientAuthentication returns a new session that authenticates a client application.
func NewClientAuthentication(clientName string, lifetime int64, scope string, user *User) *Session {
sess := NewSession(lifetime, 0)
if clientName == "" {
clientName = rnd.Name()
}
sess.SetClientName(clientName)
sess.SetScope(scope)
if user != nil {
sess.SetUser(user)
sess.SetAuthToken(rnd.AppPassword())
sess.SetProvider(authn.ProviderApplication)
sess.SetMethod(authn.MethodDefault)
} else {
sess.SetProvider(authn.ProviderAccessToken)
sess.SetMethod(authn.MethodOAuth2)
}
return sess
}
// AddClientAuthentication creates a new session for authenticating a client application.
func AddClientAuthentication(clientName string, lifetime int64, scope string, user *User) (*Session, error) {
sess := NewClientAuthentication(clientName, lifetime, scope, user)
if err := sess.Create(); err != nil {
return nil, err
}
return sess, nil
}