oidc.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "github.com/fatedier/frp/pkg/msg"
  19. "github.com/coreos/go-oidc"
  20. "golang.org/x/oauth2/clientcredentials"
  21. )
  22. type OidcClientConfig struct {
  23. // OidcClientID specifies the client ID to use to get a token in OIDC
  24. // authentication if AuthenticationMethod == "oidc". By default, this value
  25. // is "".
  26. OidcClientID string `ini:"oidc_client_id" json:"oidc_client_id"`
  27. // OidcClientSecret specifies the client secret to use to get a token in OIDC
  28. // authentication if AuthenticationMethod == "oidc". By default, this value
  29. // is "".
  30. OidcClientSecret string `ini:"oidc_client_secret" json:"oidc_client_secret"`
  31. // OidcAudience specifies the audience of the token in OIDC authentication
  32. //if AuthenticationMethod == "oidc". By default, this value is "".
  33. OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
  34. // OidcTokenEndpointURL specifies the URL which implements OIDC Token Endpoint.
  35. // It will be used to get an OIDC token if AuthenticationMethod == "oidc".
  36. // By default, this value is "".
  37. OidcTokenEndpointURL string `ini:"oidc_token_endpoint_url" json:"oidc_token_endpoint_url"`
  38. }
  39. func getDefaultOidcClientConf() OidcClientConfig {
  40. return OidcClientConfig{
  41. OidcClientID: "",
  42. OidcClientSecret: "",
  43. OidcAudience: "",
  44. OidcTokenEndpointURL: "",
  45. }
  46. }
  47. type OidcServerConfig struct {
  48. // OidcIssuer specifies the issuer to verify OIDC tokens with. This issuer
  49. // will be used to load public keys to verify signature and will be compared
  50. // with the issuer claim in the OIDC token. It will be used if
  51. // AuthenticationMethod == "oidc". By default, this value is "".
  52. OidcIssuer string `ini:"oidc_issuer" json:"oidc_issuer"`
  53. // OidcAudience specifies the audience OIDC tokens should contain when validated.
  54. // If this value is empty, audience ("client ID") verification will be skipped.
  55. // It will be used when AuthenticationMethod == "oidc". By default, this
  56. // value is "".
  57. OidcAudience string `ini:"oidc_audience" json:"oidc_audience"`
  58. // OidcSkipExpiryCheck specifies whether to skip checking if the OIDC token is
  59. // expired. It will be used when AuthenticationMethod == "oidc". By default, this
  60. // value is false.
  61. OidcSkipExpiryCheck bool `ini:"oidc_skip_expiry_check" json:"oidc_skip_expiry_check"`
  62. // OidcSkipIssuerCheck specifies whether to skip checking if the OIDC token's
  63. // issuer claim matches the issuer specified in OidcIssuer. It will be used when
  64. // AuthenticationMethod == "oidc". By default, this value is false.
  65. OidcSkipIssuerCheck bool `ini:"oidc_skip_issuer_check" json:"oidc_skip_issuer_check"`
  66. }
  67. func getDefaultOidcServerConf() OidcServerConfig {
  68. return OidcServerConfig{
  69. OidcIssuer: "",
  70. OidcAudience: "",
  71. OidcSkipExpiryCheck: false,
  72. OidcSkipIssuerCheck: false,
  73. }
  74. }
  75. type OidcAuthProvider struct {
  76. BaseConfig
  77. tokenGenerator *clientcredentials.Config
  78. }
  79. func NewOidcAuthSetter(baseCfg BaseConfig, cfg OidcClientConfig) *OidcAuthProvider {
  80. tokenGenerator := &clientcredentials.Config{
  81. ClientID: cfg.OidcClientID,
  82. ClientSecret: cfg.OidcClientSecret,
  83. Scopes: []string{cfg.OidcAudience},
  84. TokenURL: cfg.OidcTokenEndpointURL,
  85. }
  86. return &OidcAuthProvider{
  87. BaseConfig: baseCfg,
  88. tokenGenerator: tokenGenerator,
  89. }
  90. }
  91. func (auth *OidcAuthProvider) generateAccessToken() (accessToken string, err error) {
  92. tokenObj, err := auth.tokenGenerator.Token(context.Background())
  93. if err != nil {
  94. return "", fmt.Errorf("couldn't generate OIDC token for login: %v", err)
  95. }
  96. return tokenObj.AccessToken, nil
  97. }
  98. func (auth *OidcAuthProvider) SetLogin(loginMsg *msg.Login) (err error) {
  99. loginMsg.PrivilegeKey, err = auth.generateAccessToken()
  100. return err
  101. }
  102. func (auth *OidcAuthProvider) SetPing(pingMsg *msg.Ping) (err error) {
  103. if !auth.AuthenticateHeartBeats {
  104. return nil
  105. }
  106. pingMsg.PrivilegeKey, err = auth.generateAccessToken()
  107. return err
  108. }
  109. func (auth *OidcAuthProvider) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  110. if !auth.AuthenticateNewWorkConns {
  111. return nil
  112. }
  113. newWorkConnMsg.PrivilegeKey, err = auth.generateAccessToken()
  114. return err
  115. }
  116. type OidcAuthConsumer struct {
  117. BaseConfig
  118. verifier *oidc.IDTokenVerifier
  119. subjectFromLogin string
  120. }
  121. func NewOidcAuthVerifier(baseCfg BaseConfig, cfg OidcServerConfig) *OidcAuthConsumer {
  122. provider, err := oidc.NewProvider(context.Background(), cfg.OidcIssuer)
  123. if err != nil {
  124. panic(err)
  125. }
  126. verifierConf := oidc.Config{
  127. ClientID: cfg.OidcAudience,
  128. SkipClientIDCheck: cfg.OidcAudience == "",
  129. SkipExpiryCheck: cfg.OidcSkipExpiryCheck,
  130. SkipIssuerCheck: cfg.OidcSkipIssuerCheck,
  131. }
  132. return &OidcAuthConsumer{
  133. BaseConfig: baseCfg,
  134. verifier: provider.Verifier(&verifierConf),
  135. }
  136. }
  137. func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
  138. token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
  139. if err != nil {
  140. return fmt.Errorf("invalid OIDC token in login: %v", err)
  141. }
  142. auth.subjectFromLogin = token.Subject
  143. return nil
  144. }
  145. func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
  146. token, err := auth.verifier.Verify(context.Background(), privilegeKey)
  147. if err != nil {
  148. return fmt.Errorf("invalid OIDC token in ping: %v", err)
  149. }
  150. if token.Subject != auth.subjectFromLogin {
  151. return fmt.Errorf("received different OIDC subject in login and ping. "+
  152. "original subject: %s, "+
  153. "new subject: %s",
  154. auth.subjectFromLogin, token.Subject)
  155. }
  156. return nil
  157. }
  158. func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
  159. if !auth.AuthenticateHeartBeats {
  160. return nil
  161. }
  162. return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
  163. }
  164. func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
  165. if !auth.AuthenticateNewWorkConns {
  166. return nil
  167. }
  168. return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
  169. }