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}}
|
| 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) Sign(msg []byte) (s *principal.Signature, err
error) { |
| 48 return nil, nil |
| 49 } |
| 50 |
| 51 func (pImpl *PrincipalServiceImpl) GetUserBlessing(app principal.AppInstanceName
) (*principal.Blessing, error) { |
| 52 b := pImpl.psd.GetUserBlessing(app) |
| 53 return b, nil |
| 54 } |
| 55 |
| 56 func (pImpl *PrincipalServiceImpl) Create(req principal.PrincipalService_Request
) { |
| 57 stub := principal.NewPrincipalServiceStub(req, pImpl, bindings.GetAsyncW
aiter()) |
| 58 pImpl.psd.AddStubForCleanup(stub) |
| 59 go func() { |
| 60 for { |
| 61 if err := stub.ServeRequest(); err != nil { |
| 62 connectionError, ok := err.(*bindings.Connection
Error) |
| 63 if !ok || !connectionError.Closed() { |
| 64 log.Println(err) |
| 65 } |
| 66 break |
| 67 } |
| 68 } |
| 69 }() |
| 70 } |
| 71 |
| 72 type PrincipalServiceDelegate struct { |
| 73 sync.Mutex |
| 74 Ctx application.Context |
| 75 bMap map[principal.AppInstanceName]*principal.Blessing |
| 76 stubs []*bindings.Stub |
| 77 } |
| 78 |
| 79 func (psd *PrincipalServiceDelegate) Initialize(context application.Context) { |
| 80 psd.bMap = make(map[principal.AppInstanceName]*principal.Blessing) |
| 81 psd.Ctx = context |
| 82 } |
| 83 |
| 84 func (psd *PrincipalServiceDelegate) AcceptConnection(connection *application.Co
nnection) { |
| 85 app := principal.AppInstanceName{ |
| 86 Url: connection.RequestorURL(), |
| 87 Qualifier: nil, |
| 88 } |
| 89 connection.ProvideServices(&principal.PrincipalService_ServiceFactory{&P
rincipalServiceImpl{app, psd}}) |
| 90 } |
| 91 |
| 92 func (psd *PrincipalServiceDelegate) AddStubForCleanup(stub *bindings.Stub) { |
| 93 psd.Lock() |
| 94 defer psd.Unlock() |
| 95 psd.stubs = append(psd.stubs, stub) |
| 96 } |
| 97 |
| 98 func (psd *PrincipalServiceDelegate) AddUserBlessing(app principal.AppInstanceNa
me, b *principal.Blessing) { |
| 99 psd.Lock() |
| 100 defer psd.Unlock() |
| 101 psd.bMap[app] = b |
| 102 } |
| 103 |
| 104 func (psd *PrincipalServiceDelegate) GetUserBlessing(app principal.AppInstanceNa
me) *principal.Blessing { |
| 105 psd.Lock() |
| 106 defer psd.Unlock() |
| 107 return psd.bMap[app] |
| 108 } |
| 109 |
| 110 func (psd *PrincipalServiceDelegate) DeleteUserBlessing(app principal.AppInstanc
eName) { |
| 111 psd.Lock() |
| 112 defer psd.Unlock() |
| 113 delete(psd.bMap, app) |
| 114 } |
| 115 |
| 116 func (psd *PrincipalServiceDelegate) Quit() { |
| 117 psd.Lock() |
| 118 defer psd.Unlock() |
| 119 for _, stub := range psd.stubs { |
| 120 stub.Close() |
| 121 } |
| 122 } |
| 123 |
| 124 //export MojoMain |
| 125 func MojoMain(handle C.MojoHandle) C.MojoResult { |
| 126 application.Run(&PrincipalServiceDelegate{}, system.MojoHandle(handle)) |
| 127 return C.MOJO_RESULT_OK |
| 128 } |
| 129 |
| 130 func main() { |
| 131 } |
OLD | NEW |