github.com/metaworking/channeld@v0.7.3/pkg/channeld/auth.go (about)

     1  package channeld
     2  
     3  import (
     4  	"github.com/metaworking/channeld/pkg/channeldpb"
     5  	"go.uber.org/zap"
     6  )
     7  
     8  type AuthProvider interface {
     9  	DoAuth(connId ConnectionId, pit string, lt string) (channeldpb.AuthResultMessage_AuthResult, error)
    10  }
    11  
    12  // Do nothing but logging
    13  type LoggingAuthProvider struct {
    14  	Logger *zap.Logger
    15  	Msg    string
    16  }
    17  
    18  func (provider *LoggingAuthProvider) DoAuth(connId ConnectionId, pit string, lt string) (channeldpb.AuthResultMessage_AuthResult, error) {
    19  	if provider.Logger != nil {
    20  		provider.Logger.Info(provider.Msg, zap.Uint32("connId", uint32(connId)), zap.String("pit", pit), zap.String("lt", lt))
    21  	}
    22  	return channeldpb.AuthResultMessage_SUCCESSFUL, nil
    23  }
    24  
    25  // Always return AuthResultMessage_INVALID_LT
    26  type AlwaysFailAuthProvider struct{}
    27  
    28  func (provider *AlwaysFailAuthProvider) DoAuth(connId ConnectionId, pit string, lt string) (channeldpb.AuthResultMessage_AuthResult, error) {
    29  	return channeldpb.AuthResultMessage_INVALID_PIT, nil
    30  }
    31  
    32  type FixedPasswordAuthProvider struct {
    33  	Password string
    34  }
    35  
    36  func (provider *FixedPasswordAuthProvider) DoAuth(connId ConnectionId, pit string, lt string) (channeldpb.AuthResultMessage_AuthResult, error) {
    37  	if lt == provider.Password {
    38  		return channeldpb.AuthResultMessage_SUCCESSFUL, nil
    39  	} else {
    40  		return channeldpb.AuthResultMessage_INVALID_LT, nil
    41  	}
    42  }
    43  
    44  var authProvider AuthProvider
    45  
    46  // = &AlwaysFailAuthProvider{}
    47  
    48  //= &LoggingAuthProvider{
    49  // 	Logger: zap.NewExample(),
    50  // 	Msg:    "do auth",
    51  // }
    52  
    53  func SetAuthProvider(value AuthProvider) {
    54  	authProvider = value
    55  }