-
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.
feat: multiple authentication support (#49)
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
1 parent
59e0bff
commit d8f6894
Showing
11 changed files
with
525 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.out | ||
.vscode/ |
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,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 | ||
} | ||
} | ||
} |
Oops, something went wrong.