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

Add gitea auth #354

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Supported authentication methods:
* Google Sign-In (incl. Google for Work / GApps for domain) (documented [here](https://github.com/cesanta/docker_auth/blob/main/examples/reference.yml))
* [Github Sign-In](docs/auth-methods.md#github)
* Gitlab Sign-In
* Gitea basic auth
* LDAP bind ([demo](https://github.com/kwk/docker-registry-setup))
* MongoDB user collection
* MySQL/MariaDB, PostgreSQL, SQLite database table
Expand Down
1 change: 1 addition & 0 deletions auth_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ COPY . /build
WORKDIR /build
RUN make build


FROM alpine:3.17
COPY --from=build /build/auth_server /docker_auth/
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
Expand Down
102 changes: 102 additions & 0 deletions auth_server/authn/gitea_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2022 Cesanta Software Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package authn

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

"github.com/cesanta/docker_auth/auth_server/api"
)

type GiteaAuthConfig struct {
HTTPTimeout time.Duration `yaml:"http_timeout,omitempty"`
RevalidateAfter time.Duration `yaml:"revalidate_after,omitempty"`
ApiUri string `yaml:"api_uri,omitempty"`
}

type GiteaAuth struct {
config *GiteaAuthConfig
client *http.Client
}

type GiteaOrg struct {
Username string `json:"username"`
}

func NewGiteaAuth(c *GiteaAuthConfig) (*GiteaAuth, error) {
return &GiteaAuth{
config: c,
client: &http.Client{Timeout: 10 * time.Second},
}, nil
}

// func (gha *GiteaAuth) authUser(user string, password PasswordString) (err error, l Labels) {
func (gha *GiteaAuth) Authenticate(user string, password api.PasswordString) (bool, api.Labels, error) {
url := fmt.Sprintf("%s/v1/user/orgs", gha.config.ApiUri)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
err = fmt.Errorf("unable to auth: %s", err)
return false, nil, err
}
req.SetBasicAuth(user, string(password))
resp, err := gha.client.Do(req)

if err != nil {
return false, nil, err
}

if resp.StatusCode == 401 {
return false, nil, nil
} else if resp.StatusCode != 200 {
err = fmt.Errorf("wrong error code %d", resp.StatusCode)
return false, nil, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("unable to read body %s: %s", body, err)
return false, nil, err
}
resp.Body.Close()

orgs := make([]GiteaOrg, 0)
err = json.Unmarshal(body, &orgs)

if err != nil {
err = fmt.Errorf("could not unmarshal token user info %s: %s", body, err)
return false, nil, err
}

labels := api.Labels{"project": []string{}}

for _, org := range orgs {
labels["project"] = append(labels["project"], org.Username)
}

return true, labels, nil
}

func (gha *GiteaAuth) Stop() {
}

func (gha *GiteaAuth) Name() string {
return "Gitea"
}
46 changes: 19 additions & 27 deletions auth_server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,45 @@ module github.com/cesanta/docker_auth/auth_server
go 1.16

require (
cloud.google.com/go/compute v1.10.0 // indirect
cloud.google.com/go/iam v0.5.0 // indirect
cloud.google.com/go/storage v1.27.0
github.com/PuerkitoBio/goquery v1.5.1 // indirect
github.com/casbin/casbin/v2 v2.55.1
cloud.google.com/go/compute v1.13.0 // indirect
cloud.google.com/go/iam v0.7.0 // indirect
cloud.google.com/go/storage v1.28.1
github.com/casbin/casbin/v2 v2.58.0
github.com/cesanta/glog v0.0.0-20150527111657-22eb27a0ae19
github.com/cooldrip/cstrftime v0.0.0-20180425110708-e16e2f942e1e // indirect
github.com/coreos/go-oidc/v3 v3.4.0
github.com/dchest/uniuri v0.0.0-20220929095258-3027df40b6ce
github.com/dchest/uniuri v1.2.0
github.com/deckarep/golang-set v1.8.0
github.com/docker/distribution v2.8.1+incompatible
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7
github.com/go-git/go-git/v5 v5.5.0 // indirect
github.com/go-ldap/ldap v3.0.3+incompatible
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/go-stack/stack v1.8.1 // indirect
github.com/gobuffalo/genny v0.1.1 // indirect
github.com/gobuffalo/gogen v0.1.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/go-sql-driver/mysql v1.7.0
github.com/goccy/go-json v0.10.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/jstemmer/go-junit-report v1.0.0 // indirect
github.com/karrick/godirwalk v1.10.3 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/compress v1.15.12 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7
github.com/magefile/mage v1.14.0 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/montanaflynn/stats v0.6.6 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/schwarmco/go-cartesian-product v0.0.0-20180515110546-d5ee747a6dc9
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/stretchr/testify v1.7.0 // indirect
github.com/syndtr/goleveldb v1.0.0
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
go.mongodb.org/mongo-driver v1.10.2
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220926161630-eccd6366d1be
golang.org/x/net v0.0.0-20220930213112-107f3e3c3b0b
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1
golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0 // indirect
golang.org/x/sys v0.0.0-20220928140112-f11e5e49a4ec // indirect
golang.org/x/tools v0.1.12 // indirect
go.mongodb.org/mongo-driver v1.11.0
golang.org/x/crypto v0.3.0
golang.org/x/net v0.2.0
golang.org/x/oauth2 v0.2.0
golang.org/x/sync v0.1.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.98.0
google.golang.org/genproto v0.0.0-20220930163606-c98284e70a91 // indirect
google.golang.org/api v0.103.0
google.golang.org/genproto v0.0.0-20221202195650-67e5cbc046fd // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/fsnotify.v1 v1.4.7
Expand Down
Loading