Skip to content

Commit aee2d7a

Browse files
committed
feat(core): add external auth entity lookup endpoint
1 parent 94989a0 commit aee2d7a

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

core/api/api.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ func InitializeRoutes(router *gin.Engine) {
4545
router.POST("/core/token/validate", ValidateToken)
4646
router.DELETE("/core/token/:id", RevokeToken)
4747

48+
router.GET("/core/entity/external/:provider/:externalID", GetEntityByExternalAuth)
49+
4850
router.GET("/users", GetAllUsers)
4951
router.GET("/users/:id", GetUserByID)
5052
router.POST("/users", CreateOrUpdateUser)

core/api/entity.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gaucho-racing/sentinel/core/service"
7+
"github.com/gin-gonic/gin"
8+
"gorm.io/gorm"
9+
)
10+
11+
func GetEntityByExternalAuth(c *gin.Context) {
12+
provider := c.Param("provider")
13+
externalID := c.Param("externalID")
14+
entity, err := service.GetEntityByExternalAuth(provider, externalID)
15+
if err != nil {
16+
if err == gorm.ErrRecordNotFound {
17+
c.JSON(http.StatusNotFound, gin.H{"error": "entity not found"})
18+
return
19+
}
20+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
21+
return
22+
}
23+
c.JSON(http.StatusOK, entity)
24+
}

core/service/entity.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func CreatePhoneAuthForEntity(entityID string, phoneNumber string) (model.Entity
9696
return auth, nil
9797
}
9898

99+
func GetEntityByExternalAuth(provider string, externalID string) (model.Entity, error) {
100+
var auth model.EntityExternalAuth
101+
if err := database.DB.Where("provider = ? AND external_id = ?", provider, externalID).First(&auth).Error; err != nil {
102+
return model.Entity{}, err
103+
}
104+
return GetEntityByID(auth.EntityID)
105+
}
106+
99107
func GetExternalAuthForEntity(entityID string) ([]model.EntityExternalAuth, error) {
100108
auths := []model.EntityExternalAuth{}
101109
if err := database.DB.Where("entity_id = ?", entityID).Find(&auths).Error; err != nil {

0 commit comments

Comments
 (0)