Debian Codegraph @

server/backend/api_auth.go

raw
1 package backend
2
3 import (
4 "encoding/base64"
5 "fmt"
6
7 "github.com/ProtonMail/go-srp"
8 "github.com/google/uuid"
9 "github.com/henrybear327/go-proton-api"
10 )
11
12 func (b *Backend) NewAuthInfo(username string) (proton.AuthInfo, error) {
13 return withAccName(b, username, func(acc *account) (proton.AuthInfo, error) {
14 server, err := srp.NewServerFromSigned(modulus, acc.verifier, 2048)
15 if err != nil {
16 return proton.AuthInfo{}, nil
17 }
18
19 challenge, err := server.GenerateChallenge()
20 if err != nil {
21 return proton.AuthInfo{}, nil
22 }
23
24 session := uuid.NewString()
25
26 b.srpLock.Lock()
27 defer b.srpLock.Unlock()
28
29 b.srp[session] = server
30
31 return proton.AuthInfo{
32 Version: 4,
33 Modulus: modulus,
34 ServerEphemeral: base64.StdEncoding.EncodeToString(challenge),
35 Salt: base64.StdEncoding.EncodeToString(acc.salt),
36 SRPSession: session,
37 }, nil
38 })
39 }
40
41 func (b *Backend) NewAuth(username string, ephemeral, proof []byte, session string) (proton.Auth, error) {
42 return withAccName(b, username, func(acc *account) (proton.Auth, error) {
43 b.srpLock.Lock()
44 defer b.srpLock.Unlock()
45
46 server, ok := b.srp[session]
47 if !ok {
48 return proton.Auth{}, fmt.Errorf("invalid session")
49 }
50
51 delete(b.srp, session)
52
53 serverProof, err := server.VerifyProofs(ephemeral, proof)
54 if !ok {
55 return proton.Auth{}, fmt.Errorf("invalid proof: %w", err)
56 }
57
58 authUID, auth := uuid.NewString(), newAuth(b.authLife)
59
60 acc.authLock.Lock()
61 defer acc.authLock.Unlock()
62
63 acc.auth[authUID] = auth
64
65 return auth.toAuth(acc.userID, authUID, serverProof), nil
66 })
67 }
68
69 func (b *Backend) NewAuthRef(authUID, authRef string) (proton.Auth, error) {
70 b.accLock.RLock()
71 defer b.accLock.RUnlock()
72
73 for _, acc := range b.accounts {
74 acc.authLock.Lock()
75 defer acc.authLock.Unlock()
76
77 auth, ok := acc.auth[authUID]
78 if !ok {
79 continue
80 }
81
82 if auth.ref != authRef {
83 return proton.Auth{}, fmt.Errorf("invalid auth ref")
84 }
85
86 newAuth := newAuth(b.authLife)
87
88 acc.auth[authUID] = newAuth
89
90 return newAuth.toAuth(acc.userID, authUID, nil), nil
91 }
92
93 return proton.Auth{}, fmt.Errorf("invalid auth")
94 }
95
96 func (b *Backend) VerifyAuth(authUID, authAcc string) (string, error) {
97 return withAccAuth(b, authUID, authAcc, func(acc *account) (string, error) {
98 return acc.userID, nil
99 })
100 }
101
102 func (b *Backend) GetSessions(userID string) ([]proton.AuthSession, error) {
103 return withAcc(b, userID, func(acc *account) ([]proton.AuthSession, error) {
104 acc.authLock.RLock()
105 defer acc.authLock.RUnlock()
106
107 var sessions []proton.AuthSession
108
109 for authUID, auth := range acc.auth {
110 sessions = append(sessions, auth.toAuthSession(authUID))
111 }
112
113 return sessions, nil
114 })
115 }
116
117 func (b *Backend) DeleteSession(userID, authUID string) error {
118 return b.withAcc(userID, func(acc *account) error {
119 acc.authLock.Lock()
120 defer acc.authLock.Unlock()
121
122 delete(acc.auth, authUID)
123
124 return nil
125 })
126 }