-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
auth_session_client.go
42 lines (33 loc) Β· 1.11 KB
/
auth_session_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package entity
import (
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/rnd"
)
// NewClientSession returns a new session that authenticates a client application.
func NewClientSession(clientName string, expiresIn int64, scope string, grantType authn.GrantType, user *User) *Session {
sess := NewSession(expiresIn, 0)
if clientName == "" {
clientName = rnd.Name()
}
sess.SetClientName(clientName)
sess.SetScope(scope)
sess.SetGrantType(grantType)
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
}
// AddClientSession creates a new session for authenticating a client application.
func AddClientSession(clientName string, expiresIn int64, scope string, grantType authn.GrantType, user *User) (*Session, error) {
sess := NewClientSession(clientName, expiresIn, scope, grantType, user)
if err := sess.Create(); err != nil {
return nil, err
}
return sess, nil
}