Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for Cloudflare API token authentication #2

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
27 changes: 21 additions & 6 deletions updater/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package cloudflare

import (
"context"
"log/slog"
"strings"

"github.com/cloudflare/cloudflare-go"
"github.com/spf13/viper"
"github.com/we11adam/uddns/updater"
"log/slog"
"strings"
)

type Config struct {
Email string `mapstructure:"email"`
APIKey string `mapstructure:"apikey"`
Domain string `mapstructure:"domain"`
Email string `mapstructure:"email"`
APIKey string `mapstructure:"apikey"`
APIToken string `mapstructure:"apitoken"`
Domain string `mapstructure:"domain"`
}

type Cloudflare struct {
Expand All @@ -32,7 +34,20 @@ func init() {
}

func New(config *Config) (updater.Updater, error) {
api, err := cloudflare.New(config.APIKey, config.Email)
var (
api *cloudflare.API
err error
)

// If APIToken is provided, use it to create the API client
if config.APIToken != "" {
api, err = cloudflare.NewWithAPIToken(config.APIToken)
} else {
// Otherwise, use APIKey and Email to create the API client
api, err = cloudflare.New(config.APIKey, config.Email)
}

// Check if there was an error creating the API client
if err != nil {
slog.Debug("[CloudFlare] failed to create API client:", "error", err)
return nil, err
Expand Down