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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: services/vanadium/security/principal.go
diff --git a/services/vanadium/security/principal.go b/services/vanadium/security/principal.go
new file mode 100644
index 0000000000000000000000000000000000000000..31cbb390e66b974556964fd9b7f5fd63d8c05e57
--- /dev/null
+++ b/services/vanadium/security/principal.go
@@ -0,0 +1,77 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+ "crypto/ecdsa"
+ "crypto/elliptic"
+ "crypto/rand"
+ "fmt"
+ "sync"
+
+ vpkg "mojo/services/vanadium/security/interfaces/principal"
+)
+
+type principal struct {
+ private *ecdsa.PrivateKey
+ mu sync.Mutex
+ blessings map[vpkg.UserId]*wireBlessings // GUARDED_BY(mu)
+ curr *vpkg.UserId // GUARDED_BY(mu)
+}
+
+func (p *principal) publicKey() publicKey {
+ return newECDSAPublicKey(&p.private.PublicKey)
+}
+
+func (p *principal) currentBlessing() *wireBlessings {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if p.curr == nil {
+ return nil
+ }
+ return p.blessings[*p.curr]
+}
+
+func (p *principal) users() ([]vpkg.UserId, *vpkg.UserId) {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ var users []vpkg.UserId
+ for id, _ := range p.blessings {
+ users = append(users, id)
+ }
+ return users, p.curr
+}
+
+func (p *principal) addUser(id vpkg.UserId, blessing *wireBlessings) {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ p.blessings[id] = blessing
+ p.curr = &id
+}
+
+func (p *principal) setCurrentUser(id vpkg.UserId) (err *string) {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ if _, ok := p.blessings[id]; !ok {
+ str := fmt.Sprintf("UserId %v does not exist", id)
+ return &str
+ }
+ p.curr = &id
+ return
+}
+
+func (p *principal) unsetCurrentUser() {
+ p.mu.Lock()
+ defer p.mu.Unlock()
+ p.curr = nil
+}
+
+func newPrincipal() (*principal, error) {
+ priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
+ if err != nil {
+ return nil, err
+ }
+ return &principal{private: priv, blessings: make(map[vpkg.UserId]*wireBlessings)}, nil
+}

Powered by Google App Engine
This is Rietveld 408576698