authinfo.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *
  3. * Copyright 2021 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * https://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package authinfo provides authentication and authorization information that
  19. // results from the TLS handshake.
  20. package authinfo
  21. import (
  22. "errors"
  23. commonpb "github.com/google/s2a-go/internal/proto/common_go_proto"
  24. contextpb "github.com/google/s2a-go/internal/proto/s2a_context_go_proto"
  25. grpcpb "github.com/google/s2a-go/internal/proto/s2a_go_proto"
  26. "google.golang.org/grpc/credentials"
  27. )
  28. var _ credentials.AuthInfo = (*S2AAuthInfo)(nil)
  29. const s2aAuthType = "s2a"
  30. // S2AAuthInfo exposes authentication and authorization information from the
  31. // S2A session result to the gRPC stack.
  32. type S2AAuthInfo struct {
  33. s2aContext *contextpb.S2AContext
  34. commonAuthInfo credentials.CommonAuthInfo
  35. }
  36. // NewS2AAuthInfo returns a new S2AAuthInfo object from the S2A session result.
  37. func NewS2AAuthInfo(result *grpcpb.SessionResult) (credentials.AuthInfo, error) {
  38. return newS2AAuthInfo(result)
  39. }
  40. func newS2AAuthInfo(result *grpcpb.SessionResult) (*S2AAuthInfo, error) {
  41. if result == nil {
  42. return nil, errors.New("NewS2aAuthInfo given nil session result")
  43. }
  44. return &S2AAuthInfo{
  45. s2aContext: &contextpb.S2AContext{
  46. ApplicationProtocol: result.GetApplicationProtocol(),
  47. TlsVersion: result.GetState().GetTlsVersion(),
  48. Ciphersuite: result.GetState().GetTlsCiphersuite(),
  49. PeerIdentity: result.GetPeerIdentity(),
  50. LocalIdentity: result.GetLocalIdentity(),
  51. PeerCertFingerprint: result.GetPeerCertFingerprint(),
  52. LocalCertFingerprint: result.GetLocalCertFingerprint(),
  53. IsHandshakeResumed: result.GetState().GetIsHandshakeResumed(),
  54. },
  55. commonAuthInfo: credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity},
  56. }, nil
  57. }
  58. // AuthType returns the authentication type.
  59. func (s *S2AAuthInfo) AuthType() string {
  60. return s2aAuthType
  61. }
  62. // ApplicationProtocol returns the application protocol, e.g. "grpc".
  63. func (s *S2AAuthInfo) ApplicationProtocol() string {
  64. return s.s2aContext.GetApplicationProtocol()
  65. }
  66. // TLSVersion returns the TLS version negotiated during the handshake.
  67. func (s *S2AAuthInfo) TLSVersion() commonpb.TLSVersion {
  68. return s.s2aContext.GetTlsVersion()
  69. }
  70. // Ciphersuite returns the ciphersuite negotiated during the handshake.
  71. func (s *S2AAuthInfo) Ciphersuite() commonpb.Ciphersuite {
  72. return s.s2aContext.GetCiphersuite()
  73. }
  74. // PeerIdentity returns the authenticated identity of the peer.
  75. func (s *S2AAuthInfo) PeerIdentity() *commonpb.Identity {
  76. return s.s2aContext.GetPeerIdentity()
  77. }
  78. // LocalIdentity returns the local identity of the application used during
  79. // session setup.
  80. func (s *S2AAuthInfo) LocalIdentity() *commonpb.Identity {
  81. return s.s2aContext.GetLocalIdentity()
  82. }
  83. // PeerCertFingerprint returns the SHA256 hash of the peer certificate used in
  84. // the S2A handshake.
  85. func (s *S2AAuthInfo) PeerCertFingerprint() []byte {
  86. return s.s2aContext.GetPeerCertFingerprint()
  87. }
  88. // LocalCertFingerprint returns the SHA256 hash of the local certificate used
  89. // in the S2A handshake.
  90. func (s *S2AAuthInfo) LocalCertFingerprint() []byte {
  91. return s.s2aContext.GetLocalCertFingerprint()
  92. }
  93. // IsHandshakeResumed returns true if a cached session was used to resume
  94. // the handshake.
  95. func (s *S2AAuthInfo) IsHandshakeResumed() bool {
  96. return s.s2aContext.GetIsHandshakeResumed()
  97. }
  98. // SecurityLevel returns the security level of the connection.
  99. func (s *S2AAuthInfo) SecurityLevel() credentials.SecurityLevel {
  100. return s.commonAuthInfo.SecurityLevel
  101. }