56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.8.2 --package=api --generate types,chi-server,spec -o hackathon.gen.go ../schema/schema.yaml
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/hhhapz/hackathon/auth"
|
|
"github.com/hhhapz/hackathon/models"
|
|
)
|
|
|
|
// inProd tells the server whether it is in prod.
|
|
// It should be set using ldflags on build time.
|
|
var inProd bool
|
|
|
|
type Server struct {
|
|
*AuthService
|
|
}
|
|
|
|
type OAuthStore interface {
|
|
Create(callback string) (code string)
|
|
Validate(code string) (callback string, valid bool)
|
|
Remove(code string)
|
|
}
|
|
|
|
type UserStore interface {
|
|
CreateToken(ctx context.Context, user *models.User) (*models.Token, error)
|
|
RevokeToken(ctx context.Context, token string) error
|
|
RevokeUserTokens(ctx context.Context, user *models.User) (int64, error)
|
|
|
|
DecodeUser(ctx context.Context, buf []byte) (*models.User, error)
|
|
UserByToken(ctx context.Context, token string) (*models.User, error)
|
|
}
|
|
|
|
var _ ServerInterface = (*Server)(nil)
|
|
|
|
func NewServer(oaConfig auth.OAuthConfig, oaStore OAuthStore, userStore UserStore) *Server {
|
|
return &Server{
|
|
AuthService: &AuthService{
|
|
oauthConfig: oaConfig,
|
|
oauthStore: oaStore,
|
|
userStore: userStore,
|
|
},
|
|
}
|
|
}
|
|
|
|
func serverError(w http.ResponseWriter, code int, message string) {
|
|
w.WriteHeader(code)
|
|
json.NewEncoder(w).Encode(Error{
|
|
Code: code,
|
|
Message: message,
|
|
})
|
|
}
|