1
0

token.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  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 auth
  15. import (
  16. "fmt"
  17. "slices"
  18. "time"
  19. v1 "github.com/fatedier/frp/pkg/config/v1"
  20. "github.com/fatedier/frp/pkg/msg"
  21. "github.com/fatedier/frp/pkg/util/util"
  22. )
  23. type TokenAuthSetterVerifier struct {
  24. additionalAuthScopes []v1.AuthScope
  25. token string
  26. }
  27. func NewTokenAuth(additionalAuthScopes []v1.AuthScope, token string) *TokenAuthSetterVerifier {
  28. return &TokenAuthSetterVerifier{
  29. additionalAuthScopes: additionalAuthScopes,
  30. token: token,
  31. }
  32. }
  33. func (auth *TokenAuthSetterVerifier) SetLogin(loginMsg *msg.Login) error {
  34. loginMsg.PrivilegeKey = util.GetAuthKey(auth.token, loginMsg.Timestamp)
  35. return nil
  36. }
  37. func (auth *TokenAuthSetterVerifier) SetPing(pingMsg *msg.Ping) error {
  38. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
  39. return nil
  40. }
  41. pingMsg.Timestamp = time.Now().Unix()
  42. pingMsg.PrivilegeKey = util.GetAuthKey(auth.token, pingMsg.Timestamp)
  43. return nil
  44. }
  45. func (auth *TokenAuthSetterVerifier) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) error {
  46. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
  47. return nil
  48. }
  49. newWorkConnMsg.Timestamp = time.Now().Unix()
  50. newWorkConnMsg.PrivilegeKey = util.GetAuthKey(auth.token, newWorkConnMsg.Timestamp)
  51. return nil
  52. }
  53. func (auth *TokenAuthSetterVerifier) VerifyLogin(m *msg.Login) error {
  54. if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) {
  55. return fmt.Errorf("token in login doesn't match token from configuration")
  56. }
  57. return nil
  58. }
  59. func (auth *TokenAuthSetterVerifier) VerifyPing(m *msg.Ping) error {
  60. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
  61. return nil
  62. }
  63. if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) {
  64. return fmt.Errorf("token in heartbeat doesn't match token from configuration")
  65. }
  66. return nil
  67. }
  68. func (auth *TokenAuthSetterVerifier) VerifyNewWorkConn(m *msg.NewWorkConn) error {
  69. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
  70. return nil
  71. }
  72. if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) {
  73. return fmt.Errorf("token in NewWorkConn doesn't match token from configuration")
  74. }
  75. return nil
  76. }