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