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 auth providers #320

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion anthropic-model-provider/tool.gpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Name: Anthropic
Description: Model provider for Anthropic hosted Claude3 models
Metadata: envVars: OBOT_ANTHROPIC_MODEL_PROVIDER_API_KEY
Model Provider: true
Credential: ../model-provider-credential as anthropic-model-provider
Credential: ../placeholder-credential as anthropic-model-provider
Metadata: noUserAuth: anthropic-model-provider

#!sys.daemon /usr/bin/env python3 ${GPTSCRIPT_TOOL_DIR}/main.py
Expand Down
82 changes: 82 additions & 0 deletions auth-providers-common/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
module github.com/obot-platform/tools/auth-providers-common

go 1.23.4

replace github.com/oauth2-proxy/oauth2-proxy/v7 => github.com/obot-platform/oauth2-proxy/v7 v7.0.0-20241008204315-265dabe17f43

require github.com/oauth2-proxy/oauth2-proxy/v7 v7.8.1

require (
cloud.google.com/go/auth v0.10.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.5 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/bsm/redislock v0.9.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-oidc/v3 v3.11.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/ghodss/yaml v1.0.1-0.20220118164431-d8423dcdf344 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/justinas/alice v1.2.0 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mbland/hmacauth v0.0.0-20170912233209-44256dfd4bfa // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ohler55/ojg v1.24.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/api v0.205.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.31.2 // indirect
)
325 changes: 325 additions & 0 deletions auth-providers-common/go.sum

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions auth-providers-common/pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package env

import (
"fmt"
"os"
"reflect"
"strings"
)

func LoadEnvForStruct[T any](s *T) error {
v := reflect.ValueOf(s).Elem() // Dereference pointer to get struct value
t := v.Type()

if t.Kind() != reflect.Struct {
return fmt.Errorf("expected a struct, got %v", t.Kind())
}

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
varName := field.Tag.Get("env")
if varName == "" {
continue
}

// Check environment and default if set
value := os.Getenv(varName)
if value == "" {
defaultValue := field.Tag.Get("default")
if defaultValue != "" {
value = defaultValue
} else {
if field.Tag.Get("optional") == "true" {
continue
}
return fmt.Errorf("missing required environment variable %s", varName)
}
}

fieldValue := v.Field(i)
if !fieldValue.CanSet() {
return fmt.Errorf("cannot set value for field %s", field.Name)
}

// We can add more types as needed
switch field.Type.Kind() {
case reflect.String:
fieldValue.SetString(value)
case reflect.Slice:
if field.Type.Elem().Kind() == reflect.String {
fieldValue.Set(reflect.ValueOf(strings.Split(value, ",")))
} else {
return fmt.Errorf("unsupported slice element type %v for field %s", field.Type.Elem().Kind(), field.Name)
}
case reflect.Pointer:
if field.Type.Elem().Kind() == reflect.String {
fieldValue.Set(reflect.ValueOf(&value))
} else {
return fmt.Errorf("unsupported pointer element type %v for field %s", field.Type.Elem().Kind(), field.Name)
}
default:
return fmt.Errorf("unsupported type %v for field %s", field.Type.Kind(), field.Name)
}
}

return nil
}
66 changes: 66 additions & 0 deletions auth-providers-common/pkg/state/state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package state

import (
"encoding/json"
"fmt"
"net/http"
"time"

oauth2proxy "github.com/oauth2-proxy/oauth2-proxy/v7"
)

type SerializableRequest struct {
Method string `json:"method"`
URL string `json:"url"`
Header map[string][]string `json:"header"`
}

type SerializableState struct {
ExpiresOn *time.Time `json:"expiresOn"`
AccessToken string `json:"accessToken"`
PreferredUsername string `json:"preferredUsername"`
User string `json:"user"`
Email string `json:"email"`
}

func ObotGetState(p *oauth2proxy.OAuthProxy) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var sr SerializableRequest
if err := json.NewDecoder(r.Body).Decode(&sr); err != nil {
http.Error(w, fmt.Sprintf("failed to decode request body: %v", err), http.StatusBadRequest)
return
}

reqObj, err := http.NewRequest(sr.Method, sr.URL, nil)
if err != nil {
http.Error(w, fmt.Sprintf("failed to create request object: %v", err), http.StatusBadRequest)
return
}

reqObj.Header = sr.Header

state, err := p.LoadCookiedSession(reqObj)
if err != nil {
http.Error(w, fmt.Sprintf("failed to load cookied session: %v", err), http.StatusBadRequest)
return
}

if state == nil {
http.Error(w, "state is nil", http.StatusInternalServerError)
return
}

ss := SerializableState{
ExpiresOn: state.ExpiresOn,
AccessToken: state.AccessToken,
PreferredUsername: state.PreferredUsername,
User: state.User,
Email: state.Email,
}

if err := json.NewEncoder(w).Encode(ss); err != nil {
http.Error(w, fmt.Sprintf("failed to encode state: %v", err), http.StatusInternalServerError)
return
}
}
}
88 changes: 88 additions & 0 deletions github-auth-provider/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module github.com/obot-platform/tools/github-auth-provider

go 1.23.4

replace (
github.com/oauth2-proxy/oauth2-proxy/v7 => github.com/obot-platform/oauth2-proxy/v7 v7.0.0-20241008204315-265dabe17f43
github.com/obot-platform/tools/auth-providers-common => ../auth-providers-common
)

require (
github.com/oauth2-proxy/oauth2-proxy/v7 v7.8.1
github.com/obot-platform/tools/auth-providers-common v0.0.0-20241008222508-3c6174b443e7
)

require (
cloud.google.com/go/auth v0.10.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.5 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/bsm/redislock v0.9.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-oidc/v3 v3.11.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/ghodss/yaml v1.0.1-0.20220118164431-d8423dcdf344 // indirect
github.com/go-jose/go-jose/v3 v3.0.3 // indirect
github.com/go-jose/go-jose/v4 v4.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.13.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/justinas/alice v1.2.0 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mbland/hmacauth v0.0.0-20170912233209-44256dfd4bfa // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/ohler55/ojg v1.24.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.19.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.55.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/api v0.205.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241021214115-324edc3d5d38 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.35.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.31.2 // indirect
)
Loading