Skip to content

Commit

Permalink
Implemented auth-api
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 5, 2023
1 parent d30deb7 commit 6be0928
Show file tree
Hide file tree
Showing 31 changed files with 1,706 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gomod"
directory: "/" # Location of package manifests
schedule:
interval: "monthly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
52 changes: 52 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Docker
on:
push:
tags:
- 'v*'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Log in to yiwen-ai registry
uses: docker/login-action@v2
with:
registry: ${{ secrets.CR_REGISTRY }}
username: ${{ secrets.CR_USERNAME }}
password: ${{ secrets.CR_PASSWORD }}

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ secrets.CR_REGISTRY }}/ywserver/auth-api
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=semver,pattern={{raw}}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: CI
on:
push:
branches:
- 'main'
jobs:
# Test on various OS with default Go version.
tests:
name: Test on ${{matrix.os}}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
go-version: ['1.20.x']

steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Print Go version
run: go version

- name: Get dependencies
run: go get -v -t -d ./...

- name: Run tests
run: go test -v -failfast -tags=test -timeout="3m" -race ./...
40 changes: 40 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CodeQL

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: '26 4 * * 3'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@

# Go workspace file
go.work

debug/
config.toml
25 changes: 25 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# syntax=docker/dockerfile:1

FROM --platform=$BUILDPLATFORM golang:latest AS builder

WORKDIR /src
COPY config ./config
COPY keys ./keys
COPY src ./src
COPY go.mod go.sum main.go Makefile ./
RUN make build

FROM --platform=$BUILDPLATFORM ubuntu:23.04
RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone
RUN apt-get update \
&& apt-get install -y bash curl ca-certificates tzdata locales \
&& update-ca-certificates \
&& rm -rf /var/lib/apt/lists/* \
&& localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8

WORKDIR /app
COPY --from=builder /src/config ./config
COPY --from=builder /src/keys ./keys
COPY --from=builder /src/dist/auth-api ./
ENTRYPOINT ["./auth-api"]
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# options
ignore_output = &> /dev/null

.PHONY: run-dev test lint build docker

APP_NAME := auth-api
APP_PATH := github.com/yiwen-ai/auth-api
APP_VERSION := $(shell git describe --tags --always --match "v[0-9]*")
BUILD_TIME := $(shell date -u +"%FT%TZ")
BUILD_COMMIT := $(shell git rev-parse HEAD)
DOCKER_IMAGE_TAG := yiwen-ai/${APP_NAME}:latest

run-dev:
@CONFIG_FILE_PATH=${PWD}/config.toml APP_ENV=dev go run main.go

test:
@CONFIG_FILE_PATH=${PWD}/config/test.yml APP_ENV=test go test ./...

lint:
@hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
go get -u golang.org/x/lint/golint; \
fi
@golint -set_exit_status ${PKG_LIST}

build:
@mkdir -p ./dist
go build -ldflags "-X ${APP_PATH}/src/conf.AppName=${APP_NAME} \
-X ${APP_PATH}/src/conf.AppVersion=${APP_VERSION} \
-X ${APP_PATH}/src/conf.BuildTime=${BUILD_TIME} \
-X ${APP_PATH}/src/conf.GitSHA1=${BUILD_COMMIT}" \
-o ./dist/auth-api main.go

docker:
@docker build --rm -t ${DOCKER_IMAGE_TAG} .
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# authbase
# auth-api

https://auth.yiwen.ai
46 changes: 46 additions & 0 deletions cmd/keys/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"encoding/base64"
"flag"
"os"

"github.com/fxamacker/cbor/v2"
"github.com/ldclabs/cose/iana"
"github.com/ldclabs/cose/key"
"github.com/ldclabs/cose/key/hmac"
)

var kind = flag.String("kind", "state", "generate key for kind")
var out = flag.String("out", "./keys/out.key", "write key to a file")

func main() {
flag.Parse()

var err error
var k key.Key
var data []byte

switch *kind {
case "state":
k, err = hmac.GenerateKey(iana.AlgorithmHMAC_256_64)
default:
panic("unsupported kind")
}

if err == nil {
// data, err = k.MarshalCBOR()
data, err = cbor.Marshal(cbor.Tag{
Number: 55799, // self described CBOR Tag
Content: k,
})
}

if err == nil {
err = os.WriteFile(*out, []byte(base64.RawURLEncoding.EncodeToString(data)), 0644)
}

if err != nil {
panic(err)
}
}
32 changes: 32 additions & 0 deletions config/default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
env = "test" # "test", "dev", "prod"
home = "http://www.yiwen.ltd"

[log]
# Log level: "trace", "debug", "info", "warn", "error"
level = "info"

[server]
# The address to bind to.
addr = ":8080"
# The maximum number of seconds to wait for graceful shutdown.
graceful_shutdown = 10

[cookie]
# session cookie
name_prefix = "YW"
domain = "yiwen.ltd"
secure = false
expires_in = 2592000 # 60*60*24*30 seconds

[userbase]
host = "http://127.0.0.1:8080"

[keys]
cwt_pub = "./keys/ed25519-token.pub"
oauth2_state = "./keys/hmac-state.key"

[providers]
[providers.github]
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
scopes = ["SCOPE1", "SCOPE2"]
37 changes: 37 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module github.com/yiwen-ai/auth-api

go 1.20

require (
github.com/BurntSushi/toml v1.3.2
github.com/fxamacker/cbor/v2 v2.5.0-beta5
github.com/klauspost/compress v1.16.7
github.com/ldclabs/cose v1.1.1
github.com/mssola/useragent v1.0.0
github.com/rs/xid v1.5.0
github.com/teambition/compressible-go v1.0.1
github.com/teambition/gear v1.27.1
go.uber.org/dig v1.17.0
golang.org/x/oauth2 v0.7.0
)

require (
github.com/GitbookIO/mimedb v0.0.0-20180329142916-39fdfdb4def4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-http-utils/cookie v1.3.1 // indirect
github.com/go-http-utils/negotiator v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/teambition/trie-mux v1.5.2 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/text v0.10.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 6be0928

Please sign in to comment.