Initial commit
This commit is contained in:
47
internal/controllers/api.go
Normal file
47
internal/controllers/api.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"addrss/pkg/router"
|
||||
"fmt"
|
||||
|
||||
expand "github.com/openvenues/gopostal/expand"
|
||||
parser "github.com/openvenues/gopostal/parser"
|
||||
)
|
||||
|
||||
type Api struct{}
|
||||
|
||||
type ParseRequest struct {
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
func (a Api) AddRoutes() {
|
||||
router.AddPost("/v1/expand", expandAddress).Anonymous()
|
||||
router.AddPost("/v1/parse", parseAddress).Anonymous()
|
||||
}
|
||||
|
||||
func expandAddress(ctx *router.Context) {
|
||||
expansions := expand.ExpandAddress("1080 Brayden Ct. Hebron KY 41048")
|
||||
for i := 0; i < len(expansions); i++ {
|
||||
fmt.Println(expansions[i])
|
||||
}
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
|
||||
func parseAddress(ctx *router.Context) {
|
||||
pr := ParseRequest{}
|
||||
if err := ctx.Request.Bind(&pr); err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
options := parser.ParserOptions{}
|
||||
|
||||
pa := parser.ParseAddressOptions(pr.Address, options)
|
||||
addr := map[string]any{}
|
||||
|
||||
for i := 0; i < len(pa); i++ {
|
||||
addr[pa[i].Label] = pa[i].Value
|
||||
}
|
||||
|
||||
ctx.Response.OK(addr)
|
||||
}
|
||||
100
internal/controllers/auth.go
Normal file
100
internal/controllers/auth.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"addrss/pkg/auth"
|
||||
"addrss/pkg/router"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Auth struct{}
|
||||
|
||||
func (a Auth) AddRoutes() {
|
||||
router.AddPost(`/auth/guest`, guest).Anonymous()
|
||||
router.AddPost(`/auth/login`, login).Anonymous()
|
||||
router.AddPost(`/auth/logout`, logout).Anonymous()
|
||||
router.AddPost(`/auth/refresh`, refresh).Anonymous()
|
||||
router.AddPut(`/auth/password`, changePassword)
|
||||
}
|
||||
func guest(ctx *router.Context) {
|
||||
tokens, err := auth.AuthenticateGuest()
|
||||
if err != nil {
|
||||
ctx.Response.Unauthorized(err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Response.OK(tokens)
|
||||
}
|
||||
|
||||
func login(ctx *router.Context) {
|
||||
user := auth.UserLogin{}
|
||||
if err := ctx.Request.Bind(user); err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
tokens, err := auth.AuthenticateUserLogin(user)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *auth.ErrorUnauthorized:
|
||||
ctx.Response.Unauthorized(err)
|
||||
case *auth.ErrorForbidden:
|
||||
ctx.Response.Forbidden(err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Response.OK(tokens)
|
||||
}
|
||||
|
||||
func logout(ctx *router.Context) {
|
||||
// Logout uses the refresh token
|
||||
authSegments := strings.Split(ctx.Request.Header.Get("Authorization"), " ")
|
||||
|
||||
if len(authSegments) != 2 || authSegments[0] != "Bearer" {
|
||||
ctx.Response.BadRequest(fmt.Errorf("invalid token"))
|
||||
}
|
||||
|
||||
claims := auth.RefreshClaims{}
|
||||
if err := auth.ValidateJwtToken(authSegments[1], &claims); err != nil {
|
||||
ctx.Response.Unauthorized(fmt.Errorf("invalid token signature"))
|
||||
}
|
||||
|
||||
if err := auth.DestroySession(claims.Sub); err != nil {
|
||||
ctx.Response.BadRequest(fmt.Errorf("failed to destroy session"))
|
||||
}
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
|
||||
func refresh(ctx *router.Context) {
|
||||
tokens := auth.Tokens{}
|
||||
if err := ctx.Request.Bind(&tokens); err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
tokens, err := auth.AuthenticateUserRefresh(tokens.RefreshToken)
|
||||
if err != nil {
|
||||
ctx.Response.Unauthorized(err)
|
||||
}
|
||||
|
||||
ctx.Response.OK(tokens)
|
||||
}
|
||||
|
||||
func changePassword(ctx *router.Context) {
|
||||
pc := auth.PasswordChange{}
|
||||
if err := ctx.Request.Bind(&pc); err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
if pc.UserId != ctx.Claims.Sub {
|
||||
err := fmt.Errorf("user id/sub claim mismatch")
|
||||
ctx.Response.Forbidden(err)
|
||||
}
|
||||
|
||||
if err := auth.ChangePassword(pc); err != nil {
|
||||
ctx.Response.BadRequest(err)
|
||||
}
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
21
internal/controllers/guestbook.go
Normal file
21
internal/controllers/guestbook.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"addrss/pkg/router"
|
||||
)
|
||||
|
||||
type Guestbook struct{}
|
||||
|
||||
type guestbookError struct {
|
||||
Fields []string `json:"fields"`
|
||||
}
|
||||
|
||||
func (g Guestbook) AddRoutes() {
|
||||
router.AddPost("/guestbook", signGuestbook).Anonymous()
|
||||
}
|
||||
|
||||
func signGuestbook(ctx *router.Context) {
|
||||
//gb := ctx.Request.Model.(repo.Guestbook)
|
||||
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
15
internal/controllers/health.go
Normal file
15
internal/controllers/health.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"addrss/pkg/router"
|
||||
)
|
||||
|
||||
type Health struct{}
|
||||
|
||||
func (h Health) AddRoutes() {
|
||||
router.AddGet("/health", healthCheck).Anonymous()
|
||||
}
|
||||
|
||||
func healthCheck(ctx *router.Context) {
|
||||
ctx.Response.NoContent()
|
||||
}
|
||||
Reference in New Issue
Block a user