Browse Source

Allow custom JWT service expiry times

tags/v2.2.0-rc1
Daniel Oaks 4 years ago
parent
commit
9b998a7582
4 changed files with 36 additions and 14 deletions
  1. 7
    3
      conventional.yaml
  2. 7
    3
      default.yaml
  3. 15
    3
      irc/config.go
  4. 7
    5
      irc/handlers.go

+ 7
- 3
conventional.yaml View File

164
     # these services can integrate with the ircd using JSON Web Tokens (https://jwt.io)
164
     # these services can integrate with the ircd using JSON Web Tokens (https://jwt.io)
165
     # sometimes referred to with 'EXTJWT'
165
     # sometimes referred to with 'EXTJWT'
166
     jwt-services:
166
     jwt-services:
167
-        # # service name -> secret string the service uses to verify our tokens
168
-        # call-host: call-hosting-secret-token
169
-        # image-host: image-hosting-secret-token
167
+        # # service name
168
+        # call-host:
169
+        #     # custom expiry length, default is 30s
170
+        #     expiry-in-seconds: 45
171
+
172
+        #     # secret string to verify the generated tokens
173
+        #     secret: call-hosting-secret-token
170
 
174
 
171
     # allow use of the RESUME extension over plaintext connections:
175
     # allow use of the RESUME extension over plaintext connections:
172
     # do not enable this unless the ircd is only accessible over internal networks
176
     # do not enable this unless the ircd is only accessible over internal networks

+ 7
- 3
default.yaml View File

190
     # these services can integrate with the ircd using JSON Web Tokens (https://jwt.io)
190
     # these services can integrate with the ircd using JSON Web Tokens (https://jwt.io)
191
     # sometimes referred to with 'EXTJWT'
191
     # sometimes referred to with 'EXTJWT'
192
     jwt-services:
192
     jwt-services:
193
-        # # service name -> secret string the service uses to verify our tokens
194
-        # call-host: call-hosting-secret-token
195
-        # image-host: image-hosting-secret-token
193
+        # # service name
194
+        # call-host:
195
+        #     # custom expiry length, default is 30s
196
+        #     expiry-in-seconds: 45
197
+
198
+        #     # secret string to verify the generated tokens
199
+        #     secret: call-hosting-secret-token
196
 
200
 
197
     # allow use of the RESUME extension over plaintext connections:
201
     # allow use of the RESUME extension over plaintext connections:
198
     # do not enable this unless the ircd is only accessible over internal networks
202
     # do not enable this unless the ircd is only accessible over internal networks

+ 15
- 3
irc/config.go View File

471
 	MaxConnectionsPerDuration int           `yaml:"max-connections-per-duration"`
471
 	MaxConnectionsPerDuration int           `yaml:"max-connections-per-duration"`
472
 }
472
 }
473
 
473
 
474
+type JwtServiceConfig struct {
475
+	ExpiryInSeconds int64 `yaml:"expiry-in-seconds"`
476
+	Secret          string
477
+}
478
+
474
 // Config defines the overall configuration.
479
 // Config defines the overall configuration.
475
 type Config struct {
480
 type Config struct {
476
 	Network struct {
481
 	Network struct {
502
 		MOTDFormatting          bool     `yaml:"motd-formatting"`
507
 		MOTDFormatting          bool     `yaml:"motd-formatting"`
503
 		ProxyAllowedFrom        []string `yaml:"proxy-allowed-from"`
508
 		ProxyAllowedFrom        []string `yaml:"proxy-allowed-from"`
504
 		proxyAllowedFromNets    []net.IPNet
509
 		proxyAllowedFromNets    []net.IPNet
505
-		WebIRC                  []webircConfig    `yaml:"webirc"`
506
-		JwtServices             map[string]string `yaml:"jwt-services"`
507
-		MaxSendQString          string            `yaml:"max-sendq"`
510
+		WebIRC                  []webircConfig              `yaml:"webirc"`
511
+		JwtServices             map[string]JwtServiceConfig `yaml:"jwt-services"`
512
+		MaxSendQString          string                      `yaml:"max-sendq"`
508
 		MaxSendQBytes           int
513
 		MaxSendQBytes           int
509
 		AllowPlaintextResume    bool `yaml:"allow-plaintext-resume"`
514
 		AllowPlaintextResume    bool `yaml:"allow-plaintext-resume"`
510
 		Compatibility           struct {
515
 		Compatibility           struct {
922
 		config.Server.capValues[caps.Multiline] = multilineCapValue
927
 		config.Server.capValues[caps.Multiline] = multilineCapValue
923
 	}
928
 	}
924
 
929
 
930
+	// confirm jwt config
931
+	for name, info := range config.Server.JwtServices {
932
+		if info.Secret == "" {
933
+			return nil, fmt.Errorf("Could not parse jwt-services config, %s service has no secret set", name)
934
+		}
935
+	}
936
+
925
 	// handle legacy name 'bouncer' for 'multiclient' section:
937
 	// handle legacy name 'bouncer' for 'multiclient' section:
926
 	if config.Accounts.Bouncer != nil {
938
 	if config.Accounts.Bouncer != nil {
927
 		config.Accounts.Multiclient = *config.Accounts.Bouncer
939
 		config.Accounts.Multiclient = *config.Accounts.Bouncer

+ 7
- 5
irc/handlers.go View File

922
 	}
922
 	}
923
 
923
 
924
 	claims := jwt.MapClaims{
924
 	claims := jwt.MapClaims{
925
-		"exp":     time.Now().Unix() + expireInSeconds,
926
 		"iss":     server.name,
925
 		"iss":     server.name,
927
 		"sub":     client.Nick(),
926
 		"sub":     client.Nick(),
928
 		"account": accountName,
927
 		"account": accountName,
945
 		}
944
 		}
946
 	}
945
 	}
947
 
946
 
948
-	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
949
-
950
 	// we default to a secret of `*`. if you want a real secret setup a service in the config~
947
 	// we default to a secret of `*`. if you want a real secret setup a service in the config~
951
 	service := "*"
948
 	service := "*"
952
 	secret := "*"
949
 	secret := "*"
954
 		service = strings.ToLower(msg.Params[1])
951
 		service = strings.ToLower(msg.Params[1])
955
 
952
 
956
 		c := server.Config()
953
 		c := server.Config()
957
-		var exists bool
958
-		secret, exists = c.Server.JwtServices[service]
954
+		info, exists := c.Server.JwtServices[service]
959
 		if !exists {
955
 		if !exists {
960
 			rb.Add(nil, server.name, "FAIL", "EXTJWT", "NO_SUCH_SERVICE", client.t("No such service"))
956
 			rb.Add(nil, server.name, "FAIL", "EXTJWT", "NO_SUCH_SERVICE", client.t("No such service"))
961
 			return false
957
 			return false
962
 		}
958
 		}
959
+		secret = info.Secret
960
+		if info.ExpiryInSeconds != 0 {
961
+			expireInSeconds = info.ExpiryInSeconds
962
+		}
963
 	}
963
 	}
964
+	claims["exp"] = time.Now().Unix() + expireInSeconds
964
 
965
 
966
+	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
965
 	tokenString, err := token.SignedString([]byte(secret))
967
 	tokenString, err := token.SignedString([]byte(secret))
966
 
968
 
967
 	if err == nil {
969
 	if err == nil {

Loading…
Cancel
Save