您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

server.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 by David A. Golden. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package scram
  7. import "sync"
  8. // Server implements the server side of SCRAM authentication. It holds
  9. // configuration values needed to initialize new server-side conversations.
  10. // Generally, this can be persistent within an application.
  11. type Server struct {
  12. sync.RWMutex
  13. credentialCB CredentialLookup
  14. nonceGen NonceGeneratorFcn
  15. hashGen HashGeneratorFcn
  16. }
  17. func newServer(cl CredentialLookup, fcn HashGeneratorFcn) (*Server, error) {
  18. return &Server{
  19. credentialCB: cl,
  20. nonceGen: defaultNonceGenerator,
  21. hashGen: fcn,
  22. }, nil
  23. }
  24. // WithNonceGenerator replaces the default nonce generator (base64 encoding of
  25. // 24 bytes from crypto/rand) with a custom generator. This is provided for
  26. // testing or for users with custom nonce requirements.
  27. func (s *Server) WithNonceGenerator(ng NonceGeneratorFcn) *Server {
  28. s.Lock()
  29. defer s.Unlock()
  30. s.nonceGen = ng
  31. return s
  32. }
  33. // NewConversation constructs a server-side authentication conversation.
  34. // Conversations cannot be reused, so this must be called for each new
  35. // authentication attempt.
  36. func (s *Server) NewConversation() *ServerConversation {
  37. s.RLock()
  38. defer s.RUnlock()
  39. return &ServerConversation{
  40. nonceGen: s.nonceGen,
  41. hashGen: s.hashGen,
  42. credentialCB: s.credentialCB,
  43. }
  44. }