Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: services/vanadium/security/principal.go

Issue 1418013004: Principal Service: Add support for multiple user accounts (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package main
6
7 import (
8 "crypto/ecdsa"
9 "crypto/elliptic"
10 "crypto/rand"
11 "fmt"
12 "reflect"
13 "sync"
14
15 vpkg "mojo/services/vanadium/security/interfaces/principal"
16 )
17
18 type principal struct {
19 private *ecdsa.PrivateKey
20 mu sync.Mutex
21 users []vpkg.User // GUARDED_BY(mu)
22 curr *vpkg.User // GUARDED_BY(mu)
23 }
24
25 func (p *principal) publicKey() publicKey {
26 return newECDSAPublicKey(&p.private.PublicKey)
27 }
28
29 func (p *principal) getUsers() ([]vpkg.User, *vpkg.User) {
ashankar 2015/10/30 02:24:13 Just "users()" (the "get" prefix is not idiomatic
ataly 2015/11/04 00:24:30 Done.
30 p.mu.Lock()
31 defer p.mu.Unlock()
32 var users []vpkg.User
33 for _, user := range p.users {
ashankar 2015/10/30 02:24:13 How about: users := make([]vpkg.User, len(p.users)
ataly 2015/11/04 00:24:30 Done.
34 users = append(users, user)
35 }
36 return users, p.curr
37 }
38
39 func (p *principal) addUser(user vpkg.User) {
40 p.mu.Lock()
41 defer p.mu.Unlock()
42 p.users = append(p.users, user)
43 p.curr = &user
44 }
45
46 func (p *principal) setCurrentUser(user vpkg.User) (err *string) {
ashankar 2015/10/30 02:24:13 Why (err *string) instead of "err error"?
ataly 2015/11/04 00:24:30 Mojom does not support an error type. My understan
47 p.mu.Lock()
48 defer p.mu.Unlock()
49 for _, u := range p.users {
50 if !reflect.DeepEqual(u, user) {
51 str := fmt.Sprintf("User %v does not exist", user)
52 return &str
53 }
54 }
55 p.curr = &user
56 return
57 }
58
59 func (p *principal) unsetCurrentUser() {
ashankar 2015/10/30 02:24:13 clearCurrentUser?
ataly 2015/11/04 00:24:30 Done.
60 p.mu.Lock()
61 defer p.mu.Unlock()
62 p.curr = nil
63 }
64
65 func newPrincipal() (*principal, error) {
66 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
67 if err != nil {
68 return nil, err
69 }
70 return &principal{private: priv}, nil
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698