Skip to content

Commit

Permalink
feat: multiple authentication support (#49)
Browse files Browse the repository at this point in the history
This commit adds the following changes:
- Public user-facing changes for multiple auth in CallBuilder's Authenticate function
- Support for combining authentication credentials as AND/OR groups
- Unit tests to cover this feature and new assertion utilities for testing
- Updated comments
- Simplified release action

---------

Co-authored-by: Muhammad Haseeb <muhammmadhaseebisb@gmail.com>
Co-authored-by: Bisma <bisma.pervaiz@apimatic.io>
Co-authored-by: Asad Ali <asad.ali@apimatic.io>
  • Loading branch information
4 people authored Feb 14, 2024
1 parent 59e0bff commit d8f6894
Show file tree
Hide file tree
Showing 11 changed files with 525 additions and 59 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
name: Create Release
run-name: ${{ github.event.inputs.Title }} (${{ github.event.inputs.Version }})
name: Release Go Package
run-name: Publishing Package Version ${{ github.event.inputs.Version }}
on:
workflow_dispatch:
inputs:
Version:
description: "Version to be released in format: x.y.z, where x => major version, y => minor version and z => patch version"
description: "This input field requires version in format: x.y.z, where x => major version, y => minor version and z => patch version"
required: true
default: "0.1.0"
Title:
description: "Title of the release"
required: true
default: "Releasing fixes for bugs"
jobs:
create-release:
name: Creating release version ${{ github.event.inputs.Version }}
Expand All @@ -31,5 +26,5 @@ jobs:
uses: ncipollo/release-action@v1
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: ${{ github.event.inputs.Title }}
name: Release Version ${{ github.event.inputs.Version }}
body: ${{ steps.tag_version.outputs.changelog }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.out
.vscode/
73 changes: 73 additions & 0 deletions https/authenticationGroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package https

import (
"fmt"
)

const SINGLE_AUTH = "single"
const AND_AUTH = "and"
const OR_AUTH = "or"

type AuthGroup struct {
validAuthInterfaces []AuthInterface
innerGroups []AuthGroup
authType string
singleAuthKey string
errMessage string
}

func NewAuth(key string) AuthGroup {
return AuthGroup{
singleAuthKey: key,
authType: SINGLE_AUTH,
}
}

func NewOrAuth(authGroup1, authGroup2 AuthGroup, moreAuthGroups ...AuthGroup) AuthGroup {
return AuthGroup{
innerGroups: append([]AuthGroup{authGroup1, authGroup2}, moreAuthGroups...),
authType: OR_AUTH,
}
}

func NewAndAuth(authGroup1, authGroup2 AuthGroup, moreAuthGroups ...AuthGroup) AuthGroup {
return AuthGroup{
innerGroups: append([]AuthGroup{authGroup1, authGroup2}, moreAuthGroups...),
authType: AND_AUTH,
}
}

func (ag *AuthGroup) appendIndentedError(errMsg string) {
if errMsg != "" {
ag.errMessage += "\n-> " + errMsg
}
}

func (ag *AuthGroup) validate(authInterfaces map[string]AuthInterface) {
switch ag.authType {
case SINGLE_AUTH:
val, ok := authInterfaces[ag.singleAuthKey]

if !ok {
ag.appendIndentedError(fmt.Sprintf("%s is undefined!", ag.singleAuthKey))
return
}
if err := val.Validate(); err != nil {
ag.appendIndentedError(err.Error())
return
}
ag.validAuthInterfaces = append(ag.validAuthInterfaces, val)
case AND_AUTH, OR_AUTH:
for _, innerAG := range ag.innerGroups {
innerAG.validate(authInterfaces)

ag.validAuthInterfaces = append(ag.validAuthInterfaces, innerAG.validAuthInterfaces...)

if ag.authType == OR_AUTH && innerAG.errMessage == "" {
ag.errMessage = ""
return
}
ag.errMessage += innerAG.errMessage
}
}
}
Loading

0 comments on commit d8f6894

Please sign in to comment.