package pgconnimport ()func ( *PgConn) ( context.Context) error {if .config.OAuthTokenProvider == nil {returnerrors.New("OAuth authentication required but no token provider configured") } , := .config.OAuthTokenProvider()if != nil {returnfmt.Errorf("failed to obtain OAuth token: %w", ) }// https://www.rfc-editor.org/rfc/rfc7628.html#section-3.1 := []byte("n,,\x01auth=Bearer " + + "\x01\x01") := &pgproto3.SASLInitialResponse{AuthMechanism: "OAUTHBEARER",Data: , } .frontend.Send() = .flushWithPotentialWriteReadDeadlock()if != nil {return }// Leave AuthenticationOk for the startup loop, which validates authentication // policy and records completion for every authentication method. All other // responses below fail authentication and cause the connection to close. , := .peekMessage()if != nil {return }switch m := .(type) {case *pgproto3.AuthenticationOk:returnnilcase *pgproto3.AuthenticationSASLContinue:// Server sent error response in SASL continue // https://www.rfc-editor.org/rfc/rfc7628.html#section-3.2.2 // https://www.rfc-editor.org/rfc/rfc7628.html#section-3.2.3 := struct {string`json:"status"`string`json:"scope"`string`json:"openid-configuration"` }{} := json.Unmarshal(.Data, &)if != nil {returnfmt.Errorf("invalid OAuth error response from server: %w", ) }// Per RFC 7628 section 3.2.3, we should send a SASLResponse which only contains \x01. // However, since the connection will be closed anyway, we can skip thisreturnfmt.Errorf("OAuth authentication failed: %s", .)case *pgproto3.ErrorResponse:returnErrorResponseToPgError()default:returnfmt.Errorf("unexpected message type during OAuth auth: %T", ) }}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)