1
0

oidc.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. "context"
  17. "fmt"
  18. "slices"
  19. "github.com/coreos/go-oidc/v3/oidc"
  20. "golang.org/x/oauth2/clientcredentials"
  21. v1 "github.com/fatedier/frp/pkg/config/v1"
  22. "github.com/fatedier/frp/pkg/msg"
  23. )
  24. type OidcAuthProvider struct {
  25. additionalAuthScopes []v1.AuthScope
  26. tokenGenerator *clientcredentials.Config
  27. }
  28. func NewOidcAuthSetter(additionalAuthScopes []v1.AuthScope, cfg v1.AuthOIDCClientConfig) *OidcAuthProvider {
  29. eps := make(map[string][]string)
  30. for k, v := range cfg.AdditionalEndpointParams {
  31. eps[k] = []string{v}
  32. }
  33. if cfg.Audience != "" {
  34. eps["audience"] = []string{cfg.Audience}
  35. }
  36. tokenGenerator := &clientcredentials.Config{
  37. ClientID: cfg.ClientID,
  38. ClientSecret: cfg.ClientSecret,
  39. Scopes: []string{cfg.Scope},
  40. TokenURL: cfg.TokenEndpointURL,
  41. EndpointParams: eps,
  42. }
  43. return &OidcAuthProvider{
  44. additionalAuthScopes: additionalAuthScopes,
  45. tokenGenerator: tokenGenerator,
  46. }
  47. }
  48. func (auth *OidcAuthProvider) generateAccessToken() (accessToken string, err error) {
  49. tokenObj, err := auth.tokenGenerator.Token(context.Background())
  50. if err != nil {
  51. return "", fmt.Errorf("couldn't generate OIDC token for login: %v", err)
  52. }
  53. return tokenObj.AccessToken, nil
  54. }
  55. func (auth *OidcAuthProvider) SetLogin(loginMsg *msg.Login) (err error) {
  56. loginMsg.PrivilegeKey, err = auth.generateAccessToken()
  57. return err
  58. }
  59. func (auth *OidcAuthProvider) SetPing(pingMsg *msg.Ping) (err error) {
  60. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
  61. return nil
  62. }
  63. pingMsg.PrivilegeKey, err = auth.generateAccessToken()
  64. return err
  65. }
  66. func (auth *OidcAuthProvider) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  67. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
  68. return nil
  69. }
  70. newWorkConnMsg.PrivilegeKey, err = auth.generateAccessToken()
  71. return err
  72. }
  73. type OidcAuthConsumer struct {
  74. additionalAuthScopes []v1.AuthScope
  75. verifier *oidc.IDTokenVerifier
  76. subjectFromLogin string
  77. }
  78. func NewOidcAuthVerifier(additionalAuthScopes []v1.AuthScope, cfg v1.AuthOIDCServerConfig) *OidcAuthConsumer {
  79. provider, err := oidc.NewProvider(context.Background(), cfg.Issuer)
  80. if err != nil {
  81. panic(err)
  82. }
  83. verifierConf := oidc.Config{
  84. ClientID: cfg.Audience,
  85. SkipClientIDCheck: cfg.Audience == "",
  86. SkipExpiryCheck: cfg.SkipExpiryCheck,
  87. SkipIssuerCheck: cfg.SkipIssuerCheck,
  88. }
  89. return &OidcAuthConsumer{
  90. additionalAuthScopes: additionalAuthScopes,
  91. verifier: provider.Verifier(&verifierConf),
  92. }
  93. }
  94. func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
  95. token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
  96. if err != nil {
  97. return fmt.Errorf("invalid OIDC token in login: %v", err)
  98. }
  99. auth.subjectFromLogin = token.Subject
  100. return nil
  101. }
  102. func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
  103. token, err := auth.verifier.Verify(context.Background(), privilegeKey)
  104. if err != nil {
  105. return fmt.Errorf("invalid OIDC token in ping: %v", err)
  106. }
  107. if token.Subject != auth.subjectFromLogin {
  108. return fmt.Errorf("received different OIDC subject in login and ping. "+
  109. "original subject: %s, "+
  110. "new subject: %s",
  111. auth.subjectFromLogin, token.Subject)
  112. }
  113. return nil
  114. }
  115. func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
  116. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
  117. return nil
  118. }
  119. return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
  120. }
  121. func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  122. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
  123. return nil
  124. }
  125. return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
  126. }