Finally functional

This commit is contained in:
Ben 2025-07-28 22:35:06 -07:00
parent be8936a665
commit b673c640e4
Signed by: webmaster
GPG key ID: A5FCBAF34E6E8B50
12 changed files with 261 additions and 113 deletions

View file

@ -1,7 +1,7 @@
package auth
/* This file contains the AuthAuthConfig object and its methods, which handle reading
from a config file and matching requests to the whitelist. */
/* This file contains the AuthAuthConfig object and its methods, which handle
reading from a config file and matching requests to the whitelist. */
import (
"errors"
@ -9,10 +9,10 @@ import (
)
/* Stores a map between a string (bearer token) and a list of unmarshaled JSONS */
var authConfig any;
var authConfig map[string][]any = make(map[string][]any);
var authConfigSetup bool = false;
/* Opens and reads a file at the path */
/* Opens, reads, and parses a file at the path */
func SetupAuthConfig(filePath string) (err error) {
if authConfigSetup {return errors.New("Auth configuration already set up!")}
@ -21,17 +21,47 @@ func SetupAuthConfig(filePath string) (err error) {
if err != nil {return}
// Unmarshal
authConfig = unmarshalJSON(fileContents);
if authConfig == nil {return errors.New("Invalid JSON config!");}
unmarshaled := UnmarshalJSON(fileContents);
if unmarshaled == nil {return errors.New("Invalid JSON object in config file!");}
print(match(authConfig, authConfig), "\n")
// Check type assertion for base JSON object
if _, ok := unmarshaled.(map[string]any); !ok {
return errors.New("JSON is incorrect format");
}
// Loop through each bearer key
for key, val := range unmarshaled.(map[string]any) {
// Check type assertion
if _, ok := val.([]any); !ok {
return errors.New("JSON is incorrect format for key " + key);
}
// Copy over array
authConfig[key] = val.([]any);
}
// Finish setup
authConfigSetup = true;
return nil;
}
/* Gets a reference copy to the config data */
func GetAuthConfigData() (any, bool) {
/* Gets a copy to the config data */
func GetAuthConfigData() (map[string][]any, bool) {
return authConfig, authConfigSetup;
}
/* Returns true iff bearer is authorized for this request JSON */
func Authenticate(bearer string, requestJSON []byte) bool {
// Check if bearer token exists at all
if _, ok := authConfig[bearer]; !ok {return false;}
// Unmarshal JSON
unmarshaledRequest := UnmarshalJSON(requestJSON);
// Check for any object
for _, jsonObject := range authConfig[bearer] {
if match(unmarshaledRequest, jsonObject) {return true}
}
return false;
}