-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This service authorizes calls and converts extensions to users. This information can be used in Asterisk to take action.
- Loading branch information
1 parent
e66c142
commit 7880d6d
Showing
7 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package bouncer | ||
|
||
import ( | ||
"context" | ||
"github.com/crazybolillo/eryth/internal/db" | ||
"github.com/crazybolillo/eryth/internal/sqlc" | ||
"github.com/jackc/pgx/v5" | ||
"log/slog" | ||
) | ||
|
||
type Response struct { | ||
Allow bool `json:"allow"` | ||
Destination string `json:"destination"` | ||
} | ||
|
||
type Bouncer struct { | ||
*pgx.Conn | ||
} | ||
|
||
func (b *Bouncer) Check(ctx context.Context, endpoint, dialed string) Response { | ||
result := Response{ | ||
Allow: false, | ||
Destination: "", | ||
} | ||
|
||
tx, err := b.Begin(ctx) | ||
if err != nil { | ||
slog.Error("Unable to start transaction", slog.String("reason", err.Error())) | ||
return result | ||
} | ||
|
||
queries := sqlc.New(tx) | ||
destination, err := queries.GetEndpointByExtension(ctx, db.Text(dialed)) | ||
if err != nil { | ||
slog.Error("Failed to retrieve endpoint", slog.String("dialed", dialed), slog.String("reason", err.Error())) | ||
return result | ||
} | ||
|
||
return Response{ | ||
Allow: true, | ||
Destination: destination, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/crazybolillo/eryth/internal/bouncer" | ||
"github.com/go-chi/chi/v5" | ||
"log/slog" | ||
"net/http" | ||
) | ||
|
||
type CallBouncer interface { | ||
Check(ctx context.Context, endpoint, dialed string) bouncer.Response | ||
} | ||
|
||
type Authorization struct { | ||
Bouncer CallBouncer | ||
} | ||
|
||
type AuthorizationRequest struct { | ||
From string `json:"endpoint"` | ||
Extension string `json:"extension"` | ||
} | ||
|
||
func (e *Authorization) Router() chi.Router { | ||
r := chi.NewRouter() | ||
r.Post("/", e.post) | ||
|
||
return r | ||
} | ||
|
||
// @Summary Determine whether the specified action (call) is allowed or not. | ||
// @Accept json | ||
// @Produce json | ||
// @Param payload body AuthorizationRequest true "Action to be reviewed" | ||
// @Success 200 {object} bouncer.Response | ||
// @Failure 400 | ||
// @Failure 500 | ||
// @Tags bouncer | ||
// @Router /bouncer [post] | ||
func (e *Authorization) post(w http.ResponseWriter, r *http.Request) { | ||
var payload AuthorizationRequest | ||
decoder := json.NewDecoder(r.Body) | ||
err := decoder.Decode(&payload) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
response := e.Bouncer.Check(r.Context(), payload.From, payload.Extension) | ||
content, err := json.Marshal(response) | ||
if err != nil { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
_, err = w.Write(content) | ||
if err != nil { | ||
slog.Error("Failed to write response", slog.String("path", r.URL.Path), slog.String("reason", err.Error())) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters