Skip to content

Commit

Permalink
feat: implement bouncer
Browse files Browse the repository at this point in the history
This service authorizes calls and converts extensions to users. This
information can be used in Asterisk to take action.
  • Loading branch information
crazybolillo committed Jul 31, 2024
1 parent 360784b commit 24f18d4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
12 changes: 12 additions & 0 deletions db/migrations/20240730051335_user_extension_mapping.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- migrate:up
ALTER TABLE ps_endpoints ADD COLUMN sid SERIAL PRIMARY KEY;

CREATE TABLE ery_extension (
id SERIAL PRIMARY KEY,
endpoint_id SERIAL NOT NULL,
extension varchar UNIQUE,
FOREIGN KEY (endpoint_id) REFERENCES ps_endpoints(sid)
)

-- migrate:down

39 changes: 39 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
definitions:
bouncer.Response:
properties:
allow:
type: boolean
destination:
type: string
type: object
handler.AuthorizationRequest:
properties:
endpoint:
type: string
extension:
type: string
type: object
handler.createEndpointRequest:
properties:
codecs:
Expand Down Expand Up @@ -34,6 +48,31 @@ info:
title: Asterisk Administration API
version: "1.0"
paths:
/bouncer:
post:
consumes:
- application/json
parameters:
- description: Action to be reviewed
in: body
name: payload
required: true
schema:
$ref: '#/definitions/handler.AuthorizationRequest'
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/bouncer.Response'
"400":
description: Bad Request
"500":
description: Internal Server Error
summary: Determine whether the specified action (call) is allowed or not.
tags:
- bouncer
/endpoint:
post:
consumes:
Expand Down
10 changes: 10 additions & 0 deletions internal/bouncer/bouncer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bouncer

type Response struct {
Allow bool `json:"allow"`
Destination string `json:"destination"`
}

func Check(endpoint, extension string) (Response, error) {
return Response{}, nil
}
43 changes: 43 additions & 0 deletions internal/handler/authorization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package handler

import (
"encoding/json"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5"
"net/http"
)

type Authorization struct {
*pgx.Conn
}

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
}
}

0 comments on commit 24f18d4

Please sign in to comment.