Removed utils in favor of common utils

This commit is contained in:
David Allen 2024-02-27 19:45:06 -07:00
parent 2be7c3123f
commit 1e5982763f
No known key found for this signature in database
GPG key ID: 1D2A29322FBB6FCB
9 changed files with 51 additions and 119 deletions

View file

@ -3,7 +3,6 @@ package opaal
import (
"bytes"
"davidallendj/opaal/internal/oidc"
"davidallendj/opaal/internal/util"
"encoding/json"
"fmt"
"io"
@ -13,6 +12,7 @@ import (
"strings"
"time"
"github.com/davidallendj/go-utils/util"
"golang.org/x/net/publicsuffix"
)
@ -41,7 +41,7 @@ func (client *Client) IsFlowInitiated() bool {
func (client *Client) BuildAuthorizationUrl(authEndpoint string, state string, responseType string, scope []string) string {
return authEndpoint + "?" + "client_id=" + client.Id +
"&redirect_uri=" + util.URLEscape(strings.Join(client.RedirectUris, ",")) +
"&redirect_uri=" + url.QueryEscape(strings.Join(client.RedirectUris, ",")) +
"&response_type=" + responseType +
"&state=" + state +
"&scope=" + strings.Join(scope, "+") +
@ -148,7 +148,7 @@ func (client *Client) FetchTokenFromAuthenticationServer(code string, remoteUrl
func (client *Client) FetchTokenFromAuthorizationServer(remoteUrl string, jwt string, scope []string) ([]byte, error) {
// hydra endpoint: /oauth/token
data := "grant_type=" + util.URLEscape("urn:ietf:params:oauth:grant-type:jwt-bearer") +
data := "grant_type=" + url.QueryEscape("urn:ietf:params:oauth:grant-type:jwt-bearer") +
"&client_id=" + client.Id +
"&client_secret=" + client.Secret +
"&scope=" + strings.Join(scope, "+") +
@ -211,6 +211,12 @@ func (client *Client) AddTrustedIssuer(remoteUrl string, idp *oidc.IdentityProvi
return io.ReadAll(res.Body)
}
func (client *Client) AuthorizeClient(authorizeUrl string) ([]byte, error) {
bytes := []byte{}
return bytes, nil
}
func (client *Client) RegisterOAuthClient(registerUrl string, audience []string) ([]byte, error) {
// hydra endpoint: POST /clients
audience = util.QuoteArrayStrings(audience)

View file

@ -2,11 +2,12 @@ package opaal
import (
"davidallendj/opaal/internal/oidc"
"davidallendj/opaal/internal/util"
"log"
"os"
"path/filepath"
goutil "github.com/davidallendj/go-utils/util"
"gopkg.in/yaml.v2"
)
@ -23,11 +24,12 @@ type Config struct {
DecodeIdToken bool `yaml:"decode-id-token"`
DecodeAccessToken bool `yaml:"decode-access-token"`
RunOnce bool `yaml:"run-once"`
GrantType string `yaml:"grant-type"`
}
func NewConfig() Config {
return Config{
Version: util.GetCommit(),
Version: goutil.GetCommit(),
Server: Server{
Host: "127.0.0.1",
Port: 3333,
@ -38,7 +40,7 @@ func NewConfig() Config {
RedirectUris: []string{""},
},
IdentityProvider: *oidc.NewIdentityProvider(),
State: util.RandomString(20),
State: goutil.RandomString(20),
ResponseType: "code",
Scope: []string{"openid", "profile", "email"},
ActionUrls: ActionUrls{
@ -51,6 +53,7 @@ func NewConfig() Config {
DecodeIdToken: false,
DecodeAccessToken: false,
RunOnce: true,
GrantType: "authorization_code",
}
}

View file

@ -2,13 +2,14 @@ package opaal
import (
"davidallendj/opaal/internal/oidc"
"davidallendj/opaal/internal/util"
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"
"time"
"github.com/davidallendj/go-utils/util"
)
func Login(config *Config) error {

View file

@ -1,109 +0,0 @@
package util
import (
"encoding/base64"
"math/rand"
"net/url"
"os"
"os/exec"
"runtime"
"strings"
"github.com/golang-jwt/jwt"
)
func RandomString(n int) string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
b := make([]byte, n)
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func URLEscape(s string) string {
return url.QueryEscape(s)
}
func EncodeBase64(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
func DecodeJwt(encoded string) ([][]byte, error) {
// split the string into 3 segments and decode
segments := strings.Split(encoded, ".")
decoded := [][]byte{}
for _, segment := range segments {
bytes, _ := jwt.DecodeSegment(segment)
decoded = append(decoded, bytes)
}
return decoded, nil
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// https://stackoverflow.com/questions/39320371/how-start-web-server-to-open-page-in-browser-in-golang
// open opens the specified URL in the default browser of the user.
func OpenUrl(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func GetCommit() string {
bytes, err := exec.Command("git", "rev --parse HEAD").Output()
if err != nil {
return ""
}
return string(bytes)
}
func Tokenize(s string) map[string]any {
tokens := make(map[string]any)
// find token enclosed in curly brackets
return tokens
}
func QuoteArrayStrings(arr []string) []string {
for i, v := range arr {
arr[i] = "\"" + v + "\""
}
return arr
}