proxyauth.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package proxyauth provides authentication support via the
  15. // Proxy-Authorization header.
  16. package proxyauth
  17. import (
  18. "encoding/base64"
  19. "net/http"
  20. "strings"
  21. "github.com/google/martian"
  22. "github.com/google/martian/auth"
  23. )
  24. var noop = martian.Noop("proxyauth.Modifier")
  25. // Modifier is the proxy authentication modifier.
  26. type Modifier struct {
  27. reqmod martian.RequestModifier
  28. resmod martian.ResponseModifier
  29. }
  30. // NewModifier returns a new proxy authentication modifier.
  31. func NewModifier() *Modifier {
  32. return &Modifier{
  33. reqmod: noop,
  34. resmod: noop,
  35. }
  36. }
  37. // SetRequestModifier sets the request modifier.
  38. func (m *Modifier) SetRequestModifier(reqmod martian.RequestModifier) {
  39. if reqmod == nil {
  40. reqmod = noop
  41. }
  42. m.reqmod = reqmod
  43. }
  44. // SetResponseModifier sets the response modifier.
  45. func (m *Modifier) SetResponseModifier(resmod martian.ResponseModifier) {
  46. if resmod == nil {
  47. resmod = noop
  48. }
  49. m.resmod = resmod
  50. }
  51. // ModifyRequest sets the auth ID in the context from the request iff it has
  52. // not already been set and runs reqmod.ModifyRequest. If the underlying
  53. // modifier has indicated via auth error that no valid auth credentials
  54. // have been found we set ctx.SkipRoundTrip.
  55. func (m *Modifier) ModifyRequest(req *http.Request) error {
  56. ctx := martian.NewContext(req)
  57. actx := auth.FromContext(ctx)
  58. actx.SetID(id(req.Header))
  59. err := m.reqmod.ModifyRequest(req)
  60. if actx.Error() != nil {
  61. ctx.SkipRoundTrip()
  62. }
  63. return err
  64. }
  65. // ModifyResponse runs resmod.ModifyResponse and modifies the response to
  66. // include the correct status code and headers if auth error is present.
  67. //
  68. // If an error is returned from resmod.ModifyResponse it is returned.
  69. func (m *Modifier) ModifyResponse(res *http.Response) error {
  70. ctx := martian.NewContext(res.Request)
  71. actx := auth.FromContext(ctx)
  72. err := m.resmod.ModifyResponse(res)
  73. if actx.Error() != nil {
  74. res.StatusCode = http.StatusProxyAuthRequired
  75. res.Header.Set("Proxy-Authenticate", "Basic")
  76. }
  77. return err
  78. }
  79. // id returns an ID derived from the Proxy-Authorization header username and password.
  80. func id(header http.Header) string {
  81. id := strings.TrimPrefix(header.Get("Proxy-Authorization"), "Basic ")
  82. data, err := base64.StdEncoding.DecodeString(id)
  83. if err != nil {
  84. return ""
  85. }
  86. return string(data)
  87. }