github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/externals/social_assertion.go (about)

     1  package externals
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	libkb "github.com/keybase/client/go/libkb"
     8  	keybase1 "github.com/keybase/client/go/protocol/keybase1"
     9  )
    10  
    11  func MakeAssertionContext(mctx libkb.MetaContext) libkb.AssertionContext {
    12  	return libkb.MakeAssertionContext(mctx, NewProofServices(mctx.G()))
    13  }
    14  
    15  func NormalizeSocialAssertion(mctx libkb.MetaContext, s string) (keybase1.SocialAssertion, bool) {
    16  	return libkb.NormalizeSocialAssertion(MakeAssertionContext(mctx), s)
    17  }
    18  
    19  func IsSocialAssertion(mctx libkb.MetaContext, s string) bool {
    20  	return libkb.IsSocialAssertion(MakeAssertionContext(mctx), s)
    21  }
    22  
    23  func AssertionParseAndOnly(mctx libkb.MetaContext, s string) (libkb.AssertionExpression, error) {
    24  	return libkb.AssertionParseAndOnly(MakeAssertionContext(mctx), s)
    25  }
    26  
    27  func AssertionParse(mctx libkb.MetaContext, s string) (libkb.AssertionExpression, error) {
    28  	return libkb.AssertionParse(MakeAssertionContext(mctx), s)
    29  }
    30  
    31  func ParseAssertionsWithReaders(mctx libkb.MetaContext, s string) (writers, readers []libkb.AssertionExpression, err error) {
    32  	return libkb.ParseAssertionsWithReaders(MakeAssertionContext(mctx), s)
    33  }
    34  
    35  func ParseAssertionList(mctx libkb.MetaContext, s string) ([]libkb.AssertionExpression, error) {
    36  	return libkb.ParseAssertionList(MakeAssertionContext(mctx), s)
    37  }
    38  
    39  // NOTE The 'Static' methods should only be used in tests or as a basic sanity
    40  // check for the syntactical correctness of an assertion. All other callers
    41  // should use the non-static versions.
    42  // This uses only the 'static' services which exclude any parameterized proofs.
    43  // =============================================================================
    44  
    45  type staticAssertionContext struct {
    46  	ctx      context.Context
    47  	services map[string]libkb.ServiceType
    48  }
    49  
    50  // MakeStaticAssertionContext returns an AssertionContext that does not require
    51  // access to GlobalContext, but it does not understand parameterized proofs. So
    52  // only static assertions that are hardcoded in the client are valid according
    53  // to this context.
    54  func MakeStaticAssertionContext(ctx context.Context) libkb.AssertionContext {
    55  	services := make(map[string]libkb.ServiceType)
    56  	for _, st := range getStaticProofServices() {
    57  		if !useDevelProofCheckers && st.IsDevelOnly() {
    58  			continue
    59  		}
    60  		services[st.Key()] = st
    61  	}
    62  	return staticAssertionContext{ctx: ctx, services: services}
    63  }
    64  
    65  func (a staticAssertionContext) Ctx() context.Context { return a.ctx }
    66  
    67  func (a staticAssertionContext) NormalizeSocialName(service string, username string) (string, error) {
    68  	st := a.services[strings.ToLower(service)]
    69  	if st == nil {
    70  		// If we don't know about this service, normalize by going to lowercase
    71  		return strings.ToLower(username), nil
    72  	}
    73  	return st.NormalizeUsername(username)
    74  }
    75  
    76  func NormalizeSocialAssertionStatic(ctx context.Context, s string) (keybase1.SocialAssertion, bool) {
    77  	return libkb.NormalizeSocialAssertion(MakeStaticAssertionContext(ctx), s)
    78  }
    79  
    80  func AssertionParseAndOnlyStatic(ctx context.Context, s string) (libkb.AssertionExpression, error) {
    81  	return libkb.AssertionParseAndOnly(MakeStaticAssertionContext(ctx), s)
    82  }
    83  
    84  // =============================================================================