~bigbes/core-go

ref: 6c80f87886f97add9a823e9959a601c523e2147d core-go/webhooks/config.go -rw-r--r-- 1.2 KiB
6c80f878 — Drew DeVault webhooks: correct error case log format 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package webhooks

import (
	"context"
	"encoding/hex"
	"fmt"
	"time"

	"git.sr.ht/~sircmpwn/core-go/auth"
)

type AuthConfig struct {
	TokenHash string
	Grants    string
	ClientID  *string
	Expires   time.Time
}

// Pulls auth details out of the config context and returns a structure of all
// of the information necessary to build a webhook context with the same
// authentication parameters.
func NewAuthConfig(ctx context.Context) (AuthConfig, error) {
	user := auth.ForContext(ctx)
	switch user.AuthMethod {
	case auth.AUTH_OAUTH_LEGACY:
		return AuthConfig{}, fmt.Errorf("Native webhooks are not supported with legacy OAuth")
	case auth.AUTH_OAUTH2:
		ac := AuthConfig{
			TokenHash: hex.EncodeToString(user.TokenHash[:]),
			Grants:    user.BearerToken.Grants,
			Expires:   user.BearerToken.Expires.Time(),
		}
		if user.BearerToken.ClientID != "" {
			clientID := user.BearerToken.ClientID
			ac.ClientID = &clientID
		}
		return ac, nil
	case auth.AUTH_COOKIE:
		// TODO: Should this work?
		return AuthConfig{}, fmt.Errorf("Native webhooks are not supported with web authentication")
	case auth.AUTH_INTERNAL:
		// TODO: Should this work?
		panic("Internal webtoken auth is not supported")
	}
	panic("Unreachable")
}