Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions sasl/oauthbearer/oauthbearer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package oauthbearer

import (
"context"
"errors"
"fmt"

"github.com/segmentio/kafka-go/sasl"
)

// Mechanism implements the OAUTHBEARER mechanism and passes the token.
type Mechanism struct {
Token string
}

func (Mechanism) Name() string {
return "OAUTHBEARER"
}

func (m Mechanism) Start(ctx context.Context) (sasl.StateMachine, []byte, error) {
if m.Token == "" {
return nil, nil, errors.New("token must have a value")
}
header := fmt.Sprintf("n,,\x01auth=Bearer %s\x01\x01", m.Token)
byteArrayHeader := []byte(header)
return m, byteArrayHeader, nil
}

func (m Mechanism) Next(ctx context.Context, challenge []byte) (bool, []byte, error) {
if len(challenge) == 0 {
return true, nil, nil
}
return false, nil, errors.New("invalid response")
}