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

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, 2 months 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 "sync"
13
14 vpkg "mojo/services/vanadium/security/interfaces/principal"
15 )
16
17 type principal struct {
18 private *ecdsa.PrivateKey
19 mu sync.Mutex
20 blessings map[vpkg.User]*wireBlessings // GUARDED_BY(mu)
21 curr *vpkg.User // GUARDED_BY(mu)
22 }
23
24 func (p *principal) publicKey() publicKey {
25 return newECDSAPublicKey(&p.private.PublicKey)
26 }
27
28 func (p *principal) currentBlessing() *wireBlessings {
29 p.mu.Lock()
30 defer p.mu.Unlock()
31 if p.curr == nil {
32 return nil
33 }
34 return p.blessings[*p.curr]
35 }
36
37 func (p *principal) users() ([]vpkg.User, *vpkg.User) {
38 p.mu.Lock()
39 defer p.mu.Unlock()
40 var users []vpkg.User
41 for user, _ := range p.blessings {
42 users = append(users, user)
43 }
44 return users, p.curr
45 }
46
47 func (p *principal) addUser(user vpkg.User, blessing *wireBlessings) {
48 p.mu.Lock()
49 defer p.mu.Unlock()
50 p.blessings[user] = blessing
51 p.curr = &user
52 }
53
54 func (p *principal) setCurrentUser(user vpkg.User) (err *string) {
55 p.mu.Lock()
56 defer p.mu.Unlock()
57 if _, ok := p.blessings[user]; !ok {
58 str := fmt.Sprintf("User %v does not exist", user)
59 return &str
60 }
61 p.curr = &user
62 return
63 }
64
65 func (p *principal) unsetCurrentUser() {
66 p.mu.Lock()
67 defer p.mu.Unlock()
68 p.curr = nil
69 }
70
71 func newPrincipal() (*principal, error) {
72 priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
73 if err != nil {
74 return nil, err
75 }
76 return &principal{private: priv, blessings: make(map[vpkg.User]*wireBles sings)}, nil
77 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698