photoprism/internal/entity/auth_session_client.go

42 lines
1.1 KiB
Go
Raw Normal View History

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
}