@@ -14,7 +14,12 @@ import (
1414const (
1515 realm = "io.xconn.deskconn"
1616
17- ProcedureCRAVerify = "io.xconn.deskconn.account.cra.verify"
17+ procedureCRAVerify = "io.xconn.deskconn.account.cra.verify"
18+ procedureCryptosignVerify = "io.xconn.deskconn.account.cryptosign.verify"
19+
20+ accountServiceAuthRole = "xconn:deskconn:cloud:service:account"
21+ accountServiceAuthID = "deskconn-account-service"
22+ accountServicePublicKey = "c98fb454dfda50be26b74818d3c20caf6810970b9de4a01fe5cd6282603400f1"
1823)
1924
2025type Authenticator struct {
@@ -28,15 +33,13 @@ func NewAuthenticator(session *xconn.Session) *Authenticator {
2833}
2934
3035func (a * Authenticator ) Methods () []auth.Method {
31- return []auth.Method {auth .MethodAnonymous , auth .MethodCRA }
36+ return []auth.Method {auth .MethodCRA , auth .MethodCryptoSign }
3237}
3338
3439func (a * Authenticator ) Authenticate (request auth.Request ) (auth.Response , error ) {
3540 switch request .AuthMethod () {
36- case auth .Anonymous :
37- return auth .NewResponse (request .AuthID (), "anonymous" , 0 )
3841 case auth .MethodCRA :
39- callResp := a .session .Call (ProcedureCRAVerify ).Arg (request .AuthID ()).Do ()
42+ callResp := a .session .Call (procedureCRAVerify ).Arg (request .AuthID ()).Do ()
4043 if callResp .Err != nil {
4144 return nil , callResp .Err
4245 }
@@ -62,6 +65,36 @@ func (a *Authenticator) Authenticate(request auth.Request) (auth.Response, error
6265 keyLength := dict .Int64Or ("key_length" , 32 )
6366 return auth .NewCRAResponseSalted (request .AuthID (), authrole , secret , salt , int (iteration ), int (keyLength ), 0 ), nil
6467
68+ case auth .MethodCryptoSign :
69+ cryptosignRequest , ok := request .(* auth.RequestCryptoSign )
70+ if ! ok {
71+ return nil , fmt .Errorf ("invalid request" )
72+ }
73+ if cryptosignRequest .PublicKey () == accountServicePublicKey && cryptosignRequest .AuthID () == accountServiceAuthID {
74+ return auth .NewResponse (cryptosignRequest .AuthID (), accountServiceAuthRole , 0 )
75+ }
76+
77+ callResp := a .session .Call (procedureCryptosignVerify ).Args (request .AuthID (), cryptosignRequest .PublicKey ()).Do ()
78+ if callResp .Err != nil {
79+ return nil , callResp .Err
80+ }
81+
82+ dict , err := callResp .ArgDict (0 )
83+ if err != nil {
84+ return nil , err
85+ }
86+
87+ authid , err := dict .String ("authid" )
88+ if err != nil {
89+ return nil , fmt .Errorf ("failed to get authid for user(%s): %w" , request .AuthID (), err )
90+ }
91+
92+ authrole , err := dict .String ("authrole" )
93+ if err != nil {
94+ return nil , fmt .Errorf ("failed to get authrole for user(%s): %w" , request .AuthID (), err )
95+ }
96+
97+ return auth .NewResponse (authid , authrole , 0 )
6598 default :
6699 return nil , fmt .Errorf ("unsupported authentication method: %v" , request .AuthMethod ())
67100 }
@@ -77,12 +110,11 @@ func main() {
77110 AutoDiscloseCaller : true ,
78111 Roles : []xconn.RealmRole {
79112 {
80- Name : "anonymous" ,
113+ Name : accountServiceAuthRole ,
81114 Permissions : []xconn.Permission {
82115 {
83- URI : "io.xconn." ,
116+ URI : "io.xconn.deskconn. " ,
84117 MatchPolicy : "prefix" ,
85- AllowCall : true ,
86118 AllowRegister : true ,
87119 },
88120 },
@@ -91,7 +123,7 @@ func main() {
91123 Name : "user" ,
92124 Permissions : []xconn.Permission {
93125 {
94- URI : "io.xconn.deskconn.account. " ,
126+ URI : "io.xconn.deskconn." ,
95127 MatchPolicy : "prefix" ,
96128 AllowCall : true ,
97129 AllowRegister : true ,
0 commit comments