...

Source file src/golang.conradwood.net/tests/auth/auth.go

Documentation: golang.conradwood.net/tests/auth

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	au "golang.conradwood.net/apis/auth"
     8  	"golang.conradwood.net/apis/common"
     9  	"golang.conradwood.net/go-easyops/auth"
    10  	"golang.conradwood.net/go-easyops/authremote"
    11  	cm "golang.conradwood.net/go-easyops/common"
    12  	"golang.conradwood.net/go-easyops/utils"
    13  	"time"
    14  )
    15  
    16  func main() {
    17  	flag.Parse()
    18  	sname := "gotools.GoTools"
    19  	fmt.Printf("Lookup of service \"%s\" resulted in %s\n", sname, auth.GetServiceIDByName(sname))
    20  	test_speed("authremote.Context", func() { authremote.Context() })
    21  	test_speed("authremote.ContextForUser", func() { authremote.ContextForUserID("1") })
    22  
    23  	ctx := authremote.Context()
    24  	_, err := auth.SerialiseContext(ctx)
    25  	utils.Bail("failed to serialise context I just created", err)
    26  
    27  	u := auth.GetUser(ctx)
    28  	fmt.Printf("authremote.Context() got user %s\n", auth.Description(u))
    29  	am := authremote.GetAuthManagerClient()
    30  	ctx = authremote.Context()
    31  	me, err := am.WhoAmI(ctx, &common.Void{})
    32  	utils.Bail("failed to get me", err)
    33  	fmt.Printf("Result of whoami():\n")
    34  	auth.PrintUser(me)
    35  	b := cm.VerifySignature(me)
    36  	if b {
    37  		fmt.Printf("Signature Valid\n")
    38  	} else {
    39  		fmt.Printf("Signature inalid!!\n")
    40  	}
    41  
    42  	ctx, err = authremote.ContextForUser(me)
    43  	utils.Bail("failed to get context for user", err)
    44  	me = auth.GetUser(ctx)
    45  	fmt.Printf("Result of context for user with whoami():\n")
    46  	auth.PrintUser(me)
    47  
    48  	sctx, err := auth.SerialiseContext(ctx)
    49  	fmt.Printf("Serialised: %s\n", sctx[:24])
    50  	utils.Bail("failed to serialise context", err)
    51  	ctx, err = auth.RecreateContextWithTimeout(time.Duration(5)*time.Second, []byte(sctx))
    52  	utils.Bail("failed to deserialise context", err)
    53  	me = auth.GetUser(ctx)
    54  	fmt.Printf("Result of context serialise/deserialise:\n")
    55  	auth.PrintUser(me)
    56  
    57  	me, err = authremote.GetAuthManagerClient().WhoAmI(ctx, &common.Void{})
    58  	utils.Bail("failed to call authmanager.WhoAmI()", err)
    59  	fmt.Printf("Result of calling AuthManager.WhoAmi():\n")
    60  	auth.PrintUser(me)
    61  
    62  	su, err := authremote.GetAuthManagerClient().SignedGetUserByID(ctx, &au.ByIDRequest{UserID: me.ID})
    63  	utils.Bail("failed to call authmanager.WhoAmI()", err)
    64  	fmt.Printf("Result of calling AuthManager.SignedGetUserByID():\n")
    65  	auth.PrintSignedUser(su)
    66  
    67  	fmt.Printf("Done\n")
    68  
    69  }
    70  
    71  /*
    72  create another context, independent of the original one
    73  */
    74  func ForkContext(ctx context.Context) context.Context {
    75  	return ForkContextWithTimeout(ctx, time.Duration(10)*time.Second)
    76  }
    77  
    78  /*
    79  * create another context, independent of the original one, with the authentication and metadata intact and a given timeout.
    80   */
    81  func ForkContextWithTimeout(ctx context.Context, t time.Duration) context.Context {
    82  	b, err := auth.SerialiseContext(ctx)
    83  	if err != nil {
    84  		fmt.Printf("forkContext() Failed to serialise context: %s\n", err)
    85  		return ctx
    86  	}
    87  	nctx, err := auth.RecreateContextWithTimeout(t, b)
    88  	if err != nil {
    89  		fmt.Printf("forkContext() failed to recreate context: %s\n", err)
    90  		return ctx
    91  	}
    92  	return nctx
    93  
    94  }
    95  

View as plain text