oidc.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 TokenVerifier interface {
  74. Verify(context.Context, string) (*oidc.IDToken, error)
  75. }
  76. type OidcAuthConsumer struct {
  77. additionalAuthScopes []v1.AuthScope
  78. verifier TokenVerifier
  79. subjectsFromLogin []string
  80. }
  81. func NewTokenVerifier(cfg v1.AuthOIDCServerConfig) TokenVerifier {
  82. provider, err := oidc.NewProvider(context.Background(), cfg.Issuer)
  83. if err != nil {
  84. panic(err)
  85. }
  86. verifierConf := oidc.Config{
  87. ClientID: cfg.Audience,
  88. SkipClientIDCheck: cfg.Audience == "",
  89. SkipExpiryCheck: cfg.SkipExpiryCheck,
  90. SkipIssuerCheck: cfg.SkipIssuerCheck,
  91. }
  92. return provider.Verifier(&verifierConf)
  93. }
  94. func NewOidcAuthVerifier(additionalAuthScopes []v1.AuthScope, verifier TokenVerifier) *OidcAuthConsumer {
  95. return &OidcAuthConsumer{
  96. additionalAuthScopes: additionalAuthScopes,
  97. verifier: verifier,
  98. subjectsFromLogin: []string{},
  99. }
  100. }
  101. func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
  102. token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
  103. if err != nil {
  104. return fmt.Errorf("invalid OIDC token in login: %v", err)
  105. }
  106. if !slices.Contains(auth.subjectsFromLogin, token.Subject) {
  107. auth.subjectsFromLogin = append(auth.subjectsFromLogin, token.Subject)
  108. }
  109. return nil
  110. }
  111. func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
  112. token, err := auth.verifier.Verify(context.Background(), privilegeKey)
  113. if err != nil {
  114. return fmt.Errorf("invalid OIDC token in ping: %v", err)
  115. }
  116. if !slices.Contains(auth.subjectsFromLogin, token.Subject) {
  117. return fmt.Errorf("received different OIDC subject in login and ping. "+
  118. "original subjects: %s, "+
  119. "new subject: %s",
  120. auth.subjectsFromLogin, token.Subject)
  121. }
  122. return nil
  123. }
  124. func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
  125. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
  126. return nil
  127. }
  128. return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
  129. }
  130. func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  131. if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
  132. return nil
  133. }
  134. return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
  135. }