initial commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/config"
|
||||
"github.com/dusiburg/kotosoft-id/internal/db"
|
||||
"github.com/dusiburg/kotosoft-id/internal/handlers"
|
||||
apphttp "github.com/dusiburg/kotosoft-id/internal/http"
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/repositories"
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.New()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pg, err := db.ConnectPostgres(cfg.Postgres.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if err := db.RunMigrations(cfg.Postgres.MigrationFilePath, pg.Pool); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rdb, err := db.ConnectRedis(cfg.Redis.URL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
userRepo := repositories.NewUserRepository(pg.Pool)
|
||||
oauthRepo := repositories.NewOAuthRepository(pg.Pool)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
basicClient, err := ensureBasicClient(ctx, cfg, oauthRepo)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
svc := services.New(userRepo, oauthRepo, rdb.Client, services.TokenConfig{
|
||||
AccessTTL: cfg.JWT.TTL,
|
||||
RefreshTTL: cfg.JWT.TTL * 7,
|
||||
}, basicClient)
|
||||
|
||||
authHandler := handlers.NewAuthHandler(svc)
|
||||
oauthHandler := handlers.NewOAuthHandler(svc, cfg.Server.FrontendURL)
|
||||
consentHandler := handlers.NewConsentHandler(svc)
|
||||
|
||||
r := apphttp.NewRouter(authHandler, oauthHandler, consentHandler, svc, oauthRepo)
|
||||
log.Fatal(r.Run(cfg.Server.Addr))
|
||||
}
|
||||
|
||||
func ensureBasicClient(ctx context.Context, cfg *config.Conf, repo *repositories.OAuthRepository) (*models.OAuthClient, error) {
|
||||
client, err := repo.GetOAuthClientByClientID(ctx, cfg.Client.ID)
|
||||
if err == nil {
|
||||
return client, nil
|
||||
}
|
||||
|
||||
hashedSecret, err := services.HashClientSecret(cfg.Client.Secret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repo.CreateOAuthClient(ctx, models.OAuthClient{
|
||||
ClientID: cfg.Client.ID,
|
||||
ClientSecret: hashedSecret,
|
||||
Name: cfg.Client.Name,
|
||||
RedirectURIs: json.RawMessage(cfg.Client.RedirectURIsJSON()),
|
||||
GrantTypes: json.RawMessage(cfg.Client.GrantTypesJSON()),
|
||||
Scopes: json.RawMessage(cfg.Client.ScopesJSON()),
|
||||
Trusted: true,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user