Passed
Push — master ( 8b6a99...606072 )
by Tolga
01:20 queued 16s
created

balancer.getOIDCToken   A

Complexity

Conditions 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nop 3
dl 0
loc 28
rs 9.55
c 0
b 0
f 0
1
package balancer
2
3
import (
4
	"context"
5
	"fmt"
6
7
	"github.com/Permify/permify/internal/config"
8
)
9
10
// secureTokenCredentials represents a map used for storing secure tokens.
11
// These tokens require transport security.
12
type secureTokenCredentials map[string]string
13
14
// RequireTransportSecurity indicates that transport security is required for these credentials.
15
func (c secureTokenCredentials) RequireTransportSecurity() bool {
16
	return true // Transport security is required for secure tokens.
17
}
18
19
// GetRequestMetadata retrieves the current metadata (secure tokens) for a request.
20
func (c secureTokenCredentials) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
21
	return c, nil // Returns the secure tokens as metadata with no error.
22
}
23
24
// nonSecureTokenCredentials represents a map used for storing non-secure tokens.
25
// These tokens do not require transport security.
26
type nonSecureTokenCredentials map[string]string
27
28
// RequireTransportSecurity indicates that transport security is not required for these credentials.
29
func (c nonSecureTokenCredentials) RequireTransportSecurity() bool {
30
	return false // Transport security is not required for non-secure tokens.
31
}
32
33
// GetRequestMetadata retrieves the current metadata (non-secure tokens) for a request.
34
func (c nonSecureTokenCredentials) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) {
35
	return c, nil // Returns the non-secure tokens as metadata with no error.
36
}
37
38
// setupAuthn configures the authentication token based on the provided authentication method.
39
// It returns the token string and an error if any.
40
func setupAuthn(_ context.Context, authn *config.Authn) (string, error) {
41
	var token string
42
43
	switch authn.Method {
44
	case "preshared":
45
		token = authn.Preshared.Keys[0]
46
	case "oidc":
47
		return "", fmt.Errorf("unsupported authentication method: '%s'", authn.Method)
48
	default:
49
		return "", fmt.Errorf("unknown authentication method: '%s'", authn.Method)
50
	}
51
52
	return token, nil
53
}
54