Skip to content

Commit

Permalink
chore: add changes from suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chief-Rishab committed Aug 9, 2023
1 parent fc7cc7c commit ca083a9
Show file tree
Hide file tree
Showing 20 changed files with 3,474 additions and 3,448 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
VERSION := $(shell git describe --tags ${TAG})
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui
.DEFAULT_GOAL := build
PROTON_COMMIT := "10e4be7df8fd679eb32c3e52b815c55259a4c99b"
PROTON_COMMIT := "5b724a8b1ddb92e1b3b2ddcbee14e215da8b7be6"

ui:
@echo " > generating ui build"
Expand Down
27 changes: 19 additions & 8 deletions core/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import (
)

type Repository interface {
Create(ctx context.Context, domain Domain) error
Create(ctx context.Context, domain Domain) (Domain, error)
Get(ctx context.Context, id string) (Domain, error)
Update(ctx context.Context, domain Domain) (Domain, error)
List(ctx context.Context, flt Filter) ([]Domain, error)
Delete(ctx context.Context, id string) error
}

type Status string

func (s Status) String() string {
return string(s)
}

const (
Pending Status = "pending"
Verified Status = "verified"
)

type Domain struct {
ID string
Name string
OrgID string
Token string
Verified bool
VerifiedAt time.Time
CreatedAt time.Time
ID string
Name string
OrgID string
Token string
State string
UpdatedAt time.Time
CreatedAt time.Time
}
8 changes: 5 additions & 3 deletions core/domain/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package domain
import "errors"

var (
ErrNotExist = errors.New("org domain request does not exist")
ErrDomainsMisMatch = errors.New("user domain does not match the organization domain")
ErrInvalidId = errors.New("invalid domain id")
ErrNotExist = errors.New("org domain request does not exist")
ErrInvalidDomain = errors.New("invalid domain. No such host found")
ErrTXTrecordNotFound = errors.New("required TXT record not found for domain verification")
ErrDomainsMisMatch = errors.New("user domain does not match the organization domain")
ErrInvalidId = errors.New("invalid domain id")
)
4 changes: 2 additions & 2 deletions core/domain/filter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package domain

type Filter struct {
OrgID string
Verified bool
OrgID string
State string
}
31 changes: 18 additions & 13 deletions core/domain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Service struct {

const (
DNSChallenge = "_frontier-challenge.%s"
txtLength = 16
txtLength = 40
)

func NewService(repository Repository, userService UserService, orgService OrgService) *Service {
Expand All @@ -58,40 +58,45 @@ func (s Service) Delete(ctx context.Context, id string) error {
}

// Creates a record for the domain in the database and returns the TXT record that needs to be added to the DNS for the domain verification
func (s Service) Create(ctx context.Context, domain Domain) (string, error) {
func (s Service) Create(ctx context.Context, domain Domain) (Domain, error) {
txtRecord, err := generateRandomTXT()
if err != nil {
return "", err
return Domain{}, err
}

domain.Token = fmt.Sprintf(DNSChallenge, txtRecord)
if err := s.repository.Create(ctx, domain); err != nil {
return "", err
var domainResp Domain
if domainResp, err = s.repository.Create(ctx, domain); err != nil {
return Domain{}, err
}

return domain.Token, nil
return domainResp, nil
}

// VerifyDomain checks if the TXT record for the domain matches the token generated by Frontier for the domain verification
func (s Service) VerifyDomain(ctx context.Context, id string) error {
func (s Service) VerifyDomain(ctx context.Context, id string) (Domain, error) {
domain, err := s.repository.Get(ctx, id)
if err != nil {
return ErrNotExist
return Domain{}, ErrNotExist
}

txtRecords, err := net.LookupTXT(domain.Name)
if err != nil {
return err
if strings.Contains(err.Error(), "no such host") {
return Domain{}, ErrInvalidDomain
}
return Domain{}, err
}

for _, txtRecord := range txtRecords {
if txtRecord == domain.Token {
domain.Verified = true
domain.State = Verified.String()
s.repository.Update(ctx, domain)
return domain, nil
}
}

return ErrNotExist
return domain, ErrTXTrecordNotFound
}

// Join an organization as a member if the user domain matches the org whitelisted domains
Expand Down Expand Up @@ -120,8 +125,8 @@ func (s Service) Join(ctx context.Context, orgID string, userId string) error {

// check if user domain matches the org whitelisted domains
orgTrustedDomains, err := s.List(ctx, Filter{
OrgID: orgID,
Verified: true,
OrgID: orgID,
State: Verified.String(),
})
if err != nil {
return err
Expand Down
14 changes: 7 additions & 7 deletions docs/docs/apis/frontier-service-add-organization-domain.api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ api:
"example": "_frontier-challenge:1234567890123456",
"description": "The dns TXT record token to verify the domain",
},
"verified":
"state":
{
"type": "boolean",
"example": true,
"description": "true if the domain is verified, false otherwise",
"type": "string",
"example": "pending",
"description": "The domain state either pending or verified",
},
"createdAt":
{
Expand All @@ -65,12 +65,12 @@ api:
"example": "2023-06-07T05:39:56.961Z",
"description": "The time the domain whitelist request was created",
},
"verifiedAt":
"updatedAt":
{
"type": "string",
"format": "date-time",
"example": "2023-06-07T05:39:56.961Z",
"description": "The time the org domain was verified",
"description": "The time the org domain was updated",
},
},
},
Expand Down Expand Up @@ -398,7 +398,7 @@ Add a domain to an organization which if verified allows all users of the same d

A successful response.

</div><div><MimeTabs schemaType={"response"}><TabItem label={"application/json"} value={"application/json"}><SchemaTabs><TabItem label={"Schema"} value={"Schema"}><details style={{}} data-collapsed={false} open={true}><summary style={{"textAlign":"left"}}><strong>Schema</strong></summary><div style={{"textAlign":"left","marginLeft":"1rem"}}></div><ul style={{"marginLeft":"1rem"}}><SchemaItem collapsible={true} className={"schemaItem"}><details style={{}}><summary style={{}}><strong>domain</strong><span style={{"opacity":"0.6"}}> object</span></summary><div style={{"marginLeft":"1rem"}}><SchemaItem collapsible={false} name={"id"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"943e4567-e89b-12d3-a456-426655440000","description":"The domain id"}}></SchemaItem><SchemaItem collapsible={false} name={"name"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"raystack.org","description":"The domain name"}}></SchemaItem><SchemaItem collapsible={false} name={"orgId"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"123e4567-e89b-12d3-a456-426655440000","description":"The organization id"}}></SchemaItem><SchemaItem collapsible={false} name={"token"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"_frontier-challenge:1234567890123456","description":"The dns TXT record token to verify the domain"}}></SchemaItem><SchemaItem collapsible={false} name={"verified"} required={false} schemaName={"boolean"} qualifierMessage={undefined} schema={{"type":"boolean","example":true,"description":"true if the domain is verified, false otherwise"}}></SchemaItem><SchemaItem collapsible={false} name={"createdAt"} required={false} schemaName={"date-time"} qualifierMessage={undefined} schema={{"type":"string","format":"date-time","example":"2023-06-07T05:39:56.961Z","description":"The time the domain whitelist request was created"}}></SchemaItem><SchemaItem collapsible={false} name={"verifiedAt"} required={false} schemaName={"date-time"} qualifierMessage={undefined} schema={{"type":"string","format":"date-time","example":"2023-06-07T05:39:56.961Z","description":"The time the org domain was verified"}}></SchemaItem></div></details></SchemaItem></ul></details></TabItem><TabItem label={"Example (from schema)"} value={"Example (from schema)"}><ResponseSamples responseExample={"{\n \"domain\": {\n \"id\": \"943e4567-e89b-12d3-a456-426655440000\",\n \"name\": \"raystack.org\",\n \"orgId\": \"123e4567-e89b-12d3-a456-426655440000\",\n \"token\": \"_frontier-challenge:1234567890123456\",\n \"verified\": true,\n \"createdAt\": \"2023-06-07T05:39:56.961Z\",\n \"verifiedAt\": \"2023-06-07T05:39:56.961Z\"\n }\n}"} language={"json"}></ResponseSamples></TabItem></SchemaTabs></TabItem></MimeTabs></div></TabItem><TabItem label={"400"} value={"400"}><div>
</div><div><MimeTabs schemaType={"response"}><TabItem label={"application/json"} value={"application/json"}><SchemaTabs><TabItem label={"Schema"} value={"Schema"}><details style={{}} data-collapsed={false} open={true}><summary style={{"textAlign":"left"}}><strong>Schema</strong></summary><div style={{"textAlign":"left","marginLeft":"1rem"}}></div><ul style={{"marginLeft":"1rem"}}><SchemaItem collapsible={true} className={"schemaItem"}><details style={{}}><summary style={{}}><strong>domain</strong><span style={{"opacity":"0.6"}}> object</span></summary><div style={{"marginLeft":"1rem"}}><SchemaItem collapsible={false} name={"id"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"943e4567-e89b-12d3-a456-426655440000","description":"The domain id"}}></SchemaItem><SchemaItem collapsible={false} name={"name"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"raystack.org","description":"The domain name"}}></SchemaItem><SchemaItem collapsible={false} name={"orgId"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"123e4567-e89b-12d3-a456-426655440000","description":"The organization id"}}></SchemaItem><SchemaItem collapsible={false} name={"token"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"_frontier-challenge:1234567890123456","description":"The dns TXT record token to verify the domain"}}></SchemaItem><SchemaItem collapsible={false} name={"state"} required={false} schemaName={"string"} qualifierMessage={undefined} schema={{"type":"string","example":"pending","description":"The domain state either pending or verified"}}></SchemaItem><SchemaItem collapsible={false} name={"createdAt"} required={false} schemaName={"date-time"} qualifierMessage={undefined} schema={{"type":"string","format":"date-time","example":"2023-06-07T05:39:56.961Z","description":"The time the domain whitelist request was created"}}></SchemaItem><SchemaItem collapsible={false} name={"updatedAt"} required={false} schemaName={"date-time"} qualifierMessage={undefined} schema={{"type":"string","format":"date-time","example":"2023-06-07T05:39:56.961Z","description":"The time the org domain was updated"}}></SchemaItem></div></details></SchemaItem></ul></details></TabItem><TabItem label={"Example (from schema)"} value={"Example (from schema)"}><ResponseSamples responseExample={"{\n \"domain\": {\n \"id\": \"943e4567-e89b-12d3-a456-426655440000\",\n \"name\": \"raystack.org\",\n \"orgId\": \"123e4567-e89b-12d3-a456-426655440000\",\n \"token\": \"_frontier-challenge:1234567890123456\",\n \"state\": \"pending\",\n \"createdAt\": \"2023-06-07T05:39:56.961Z\",\n \"updatedAt\": \"2023-06-07T05:39:56.961Z\"\n }\n}"} language={"json"}></ResponseSamples></TabItem></SchemaTabs></TabItem></MimeTabs></div></TabItem><TabItem label={"400"} value={"400"}><div>

Bad Request - The request was malformed or contained invalid parameters.

Expand Down
Loading

0 comments on commit ca083a9

Please sign in to comment.