Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "fmt" | |
| 9 "log" | |
| 10 "sync" | |
| 11 | |
| 12 "mojo/public/go/application" | |
| 13 "mojo/public/go/bindings" | |
| 14 "mojo/public/go/system" | |
| 15 auth "mojo/services/authentication/public/interfaces/authentication" | |
| 16 "mojo/services/vanadium/security/public/interfaces/principal" | |
| 17 ) | |
| 18 | |
| 19 //#include "mojo/public/c/system/types.h" | |
| 20 import "C" | |
| 21 | |
| 22 type PrincipalServiceImpl struct { | |
| 23 app principal.AppInstanceName | |
| 24 psd *PrincipalServiceDelegate | |
| 25 } | |
| 26 | |
| 27 func (pImpl *PrincipalServiceImpl) Login() (b *principal.Blessing, err error) { | |
| 28 authReq, authPtr := auth.CreateMessagePipeForAuthenticationService() | |
| 29 pImpl.psd.Ctx.ConnectToApplication("mojo:authentication").ConnectToService( &authReq) | |
| 30 authProxy := auth.NewAuthenticationServiceProxy(authPtr, bindings.GetAsyncW aiter()) | |
| 31 name, errString, _ := authProxy.SelectAccount(false /*return_last_selected */) | |
| 32 if name != nil { | |
| 33 cert := []principal.Certificate{principal.Certificate{Extension: *name}} | |
|
ashankar
2015/08/19 05:50:38
What about the public key? It seems that we're ret
gautham
2015/08/19 17:45:51
Yes. Key management to follow.
| |
| 34 b = &principal.Blessing{cert} | |
| 35 pImpl.psd.AddUserBlessing(pImpl.app, b) | |
| 36 } else { | |
| 37 err = fmt.Errorf("Failed to authenticate user:%s", errString) | |
| 38 } | |
| 39 return | |
| 40 } | |
| 41 | |
| 42 func (pImpl *PrincipalServiceImpl) Logout() (err error) { | |
| 43 pImpl.psd.DeleteUserBlessing(pImpl.app) | |
| 44 return | |
| 45 } | |
| 46 | |
| 47 func (pImpl *PrincipalServiceImpl) GetUserBlessing(app principal.AppInstanceName ) (*principal.Blessing, error) { | |
| 48 b := pImpl.psd.GetUserBlessing(app) | |
|
ashankar
2015/08/19 05:50:38
Optional:
return pImpl.psd.GetUserBlessing(app), n
gautham
2015/08/19 17:45:51
Done.
| |
| 49 return b, nil | |
| 50 } | |
| 51 | |
| 52 func (pImpl *PrincipalServiceImpl) Create(req principal.PrincipalService_Request ) { | |
| 53 stub := principal.NewPrincipalServiceStub(req, pImpl, bindings.GetAsyncW aiter()) | |
| 54 pImpl.psd.AddStubForCleanup(stub) | |
| 55 go func() { | |
| 56 for { | |
| 57 if err := stub.ServeRequest(); err != nil { | |
| 58 connectionError, ok := err.(*bindings.Connection Error) | |
| 59 if !ok || !connectionError.Closed() { | |
| 60 log.Println(err) | |
| 61 } | |
| 62 break | |
| 63 } | |
| 64 } | |
| 65 }() | |
| 66 } | |
| 67 | |
| 68 type PrincipalServiceDelegate struct { | |
| 69 sync.Mutex | |
|
ashankar
2015/08/19 05:50:38
Instead of embedding a Mutex I would recommend mak
gautham
2015/08/19 17:45:51
Done.
| |
| 70 Ctx application.Context | |
| 71 bMap map[principal.AppInstanceName]*principal.Blessing | |
| 72 stubs []*bindings.Stub | |
| 73 } | |
| 74 | |
| 75 func (psd *PrincipalServiceDelegate) Initialize(context application.Context) { | |
| 76 psd.bMap = make(map[principal.AppInstanceName]*principal.Blessing) | |
| 77 psd.Ctx = context | |
| 78 } | |
| 79 | |
| 80 func (psd *PrincipalServiceDelegate) AcceptConnection(connection *application.Co nnection) { | |
| 81 app := principal.AppInstanceName{ | |
| 82 Url: connection.RequestorURL(), | |
| 83 Qualifier: nil, | |
| 84 } | |
| 85 connection.ProvideServices(&principal.PrincipalService_ServiceFactory{&P rincipalServiceImpl{app, psd}}) | |
|
ashankar
2015/08/19 05:50:38
gofmt?
gautham
2015/08/19 17:45:51
Done.
| |
| 86 } | |
| 87 | |
| 88 func (psd *PrincipalServiceDelegate) AddStubForCleanup(stub *bindings.Stub) { | |
| 89 psd.Lock() | |
| 90 defer psd.Unlock() | |
| 91 psd.stubs = append(psd.stubs, stub) | |
| 92 } | |
| 93 | |
| 94 func (psd *PrincipalServiceDelegate) AddUserBlessing(app principal.AppInstanceNa me, b *principal.Blessing) { | |
|
ashankar
2015/08/19 05:50:38
Do these methods need to be exported?
gautham
2015/08/19 17:45:51
Done.
| |
| 95 psd.Lock() | |
| 96 defer psd.Unlock() | |
| 97 psd.bMap[app] = b | |
| 98 } | |
| 99 | |
| 100 func (psd *PrincipalServiceDelegate) GetUserBlessing(app principal.AppInstanceNa me) *principal.Blessing { | |
| 101 psd.Lock() | |
| 102 defer psd.Unlock() | |
| 103 return psd.bMap[app] | |
| 104 } | |
| 105 | |
| 106 func (psd *PrincipalServiceDelegate) DeleteUserBlessing(app principal.AppInstanc eName) { | |
| 107 psd.Lock() | |
| 108 defer psd.Unlock() | |
| 109 delete(psd.bMap, app) | |
| 110 } | |
| 111 | |
| 112 func (psd *PrincipalServiceDelegate) Quit() { | |
| 113 psd.Lock() | |
| 114 defer psd.Unlock() | |
| 115 for _, stub := range psd.stubs { | |
| 116 stub.Close() | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 //export MojoMain | |
| 121 func MojoMain(handle C.MojoHandle) C.MojoResult { | |
| 122 application.Run(&PrincipalServiceDelegate{}, system.MojoHandle(handle)) | |
| 123 return C.MOJO_RESULT_OK | |
| 124 } | |
| 125 | |
| 126 func main() { | |
| 127 } | |
| OLD | NEW |