Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeker12 committed Jul 10, 2024
1 parent dcf2d5f commit ab046a3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 55 deletions.
3 changes: 0 additions & 3 deletions jamf/jamfprointegration/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ const (
)

type authInterface interface {
// Token Operations
getNewToken() error
getTokenString() string
getExpiryTime() time.Time

// Token Utils
tokenExpired() bool
tokenInBuffer() bool
tokenEmpty() bool
Expand Down
26 changes: 10 additions & 16 deletions jamf/jamfprointegration/auth_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,24 @@ import (
"go.uber.org/zap"
)

// basicAuth struct implements authInterface for this integration
type basicAuth struct {
Sugar *zap.SugaredLogger

// Set
baseDomain string
username string
password string
bufferPeriod time.Duration
hideSensitiveData bool

// Computed
// basicToken string
Sugar *zap.SugaredLogger
baseDomain string
username string
password string
bufferPeriod time.Duration
hideSensitiveData bool
bearerToken string
bearerTokenExpiryTime time.Time
}

// basicAuthResponse serves as a json structure map for the basicAuth response from Jamf.
type basicAuthResponse struct {
Token string `json:"token"`
Expires time.Time `json:"expires"`
}

// Operations

// getNewToken obtains a new bearer token from the authentication server.
// This function constructs a new HTTP request to the bearer token endpoint using the basic authentication credentials,
// sends the request, and updates the basicAuth instance with the new bearer token and its expiry time.
Expand All @@ -50,14 +45,14 @@ type basicAuthResponse struct {
// TODO migrate strings
func (a *basicAuth) getNewToken() error {
client := http.Client{}

completeBearerEndpoint := a.baseDomain + bearerTokenEndpoint
a.Sugar.Debugf("bearer endpoint constructed: %s", completeBearerEndpoint)

req, err := http.NewRequest("POST", completeBearerEndpoint, nil)
if err != nil {
return err
}

a.Sugar.Debugf("bearer token request constructed: %+v", req)

req.SetBasicAuth(a.username, a.password)
Expand All @@ -67,6 +62,7 @@ func (a *basicAuth) getNewToken() error {
return err
}
defer resp.Body.Close()

a.Sugar.Debugf("bearer token request made: %v", resp.StatusCode)

if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -110,8 +106,6 @@ func (a *basicAuth) getExpiryTime() time.Time {
return a.bearerTokenExpiryTime
}

// Utils

// tokenExpired checks if the current bearer token has expired.
// This function compares the current time with the bearer token's expiry time to determine if the token has expired.
//
Expand Down
28 changes: 11 additions & 17 deletions jamf/jamfprointegration/auth_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@ import (
"go.uber.org/zap"
)

// oauth implements the authInterface for Oauth2 support
type oauth struct {
Sugar *zap.SugaredLogger

// Set
Sugar *zap.SugaredLogger
baseDomain string
clientId string
clientSecret string
bufferPeriod time.Duration
hideSensitiveData bool

// Computed
expiryTime time.Time
token string
expiryTime time.Time
token string
}

// OAuthResponse represents the response structure when obtaining an OAuth access token from JamfPro.
Expand All @@ -36,13 +33,12 @@ type OAuthResponse struct {
RefreshToken string `json:"refresh_token,omitempty"`
}

// Operations

// TODO migrate strings

// getNewToken updates the held token and expiry information
func (a *oauth) getNewToken() error {
client := http.Client{}
data := url.Values{}

data.Set("client_id", a.clientId)
data.Set("client_secret", a.clientSecret)
data.Set("grant_type", "client_credentials")
Expand Down Expand Up @@ -99,29 +95,27 @@ func (a *oauth) getNewToken() error {
return nil
}

// TODO func comment
// getTokenString returns the current token as a string
func (a *oauth) getTokenString() string {
return a.token
}

// TODO func comment
// getExpiryTime returns the current token's expiry time as a time.Time var.
func (a *oauth) getExpiryTime() time.Time {
return a.expiryTime
}

// Utils

// TODO func comment
// tokenExpired returns a bool denoting if the current token expiry time has passed.
func (a *oauth) tokenExpired() bool {
return a.expiryTime.Before(time.Now())
}

// TODO func comment
// tokenInBuffer returns a bool denoting if the current token's duration until expiry is within the buffer period
func (a *oauth) tokenInBuffer() bool {
return time.Until(a.expiryTime) <= a.bufferPeriod
}

// TODO func comment
// tokenEmpty returns a bool denoting if the current token string is empty.
func (a *oauth) tokenEmpty() bool {
return a.token == ""
}
8 changes: 4 additions & 4 deletions jamf/jamfprointegration/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"go.uber.org/zap"
)

// TODO migrate strings
// BuildWithOAuth is a helper function allowing the full construct of a Jamf Integration using OAuth2
func BuildWithOAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, clientId string, clientSecret string, hideSensitiveData bool) (*Integration, error) {
integration := Integration{
BaseDomain: jamfBaseDomain,
Expand All @@ -20,7 +20,7 @@ func BuildWithOAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPerio
return &integration, err
}

// TODO migrate strings
// BuildWithBasicAuth is a helper function allowing the full construct of a Jamf Integration using BasicAuth
func BuildWithBasicAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferPeriod time.Duration, username string, password string, hideSensitiveData bool) (*Integration, error) {
integration := Integration{
BaseDomain: jamfBaseDomain,
Expand All @@ -34,7 +34,7 @@ func BuildWithBasicAuth(jamfBaseDomain string, Sugar *zap.SugaredLogger, bufferP
return &integration, err
}

// TODO migrate strings
// BuildOAuth is a helper which returns just a configured OAuth interface
func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPeriod time.Duration, hideSensitiveData bool) {
authInterface := oauth{
clientId: clientId,
Expand All @@ -48,7 +48,7 @@ func (j *Integration) BuildOAuth(clientId string, clientSecret string, bufferPer
j.auth = &authInterface
}

// TODO migrate strings
// BuildBasicAuth is a helper which returns just a configured Basic Auth interface/
func (j *Integration) BuildBasicAuth(username string, password string, bufferPeriod time.Duration, hideSensitiveData bool) {
authInterface := basicAuth{
username: username,
Expand Down
20 changes: 8 additions & 12 deletions jamf/jamfprointegration/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,42 @@ type Integration struct {
auth authInterface
}

// Info

// TODO migrate strings
// getFQDN returns just the FQDN // TODO remove the "get"
func (j *Integration) GetFQDN() string {
return j.BaseDomain
}

// TODO this comment
// constructURL appends any endpoint to the FQDN
func (j *Integration) ConstructURL(endpoint string) string {
return j.GetFQDN() + endpoint
}

// TODO migrate strings
// GetAuthMethodDescriptor returns a single string describing the auth method for debug and logging purposes
func (j *Integration) GetAuthMethodDescriptor() string {
return j.AuthMethodDescriptor
}

// Utilities

// TODO migrate strings
// CheckRefreshToken ensures the token is valid and refreshes if it is not.
func (j *Integration) CheckRefreshToken() error {
return j.checkRefreshToken()
}

// TODO migrate strings
// PrepRequestParamsAndAuth applies any parameters and authentication headers to a http.Request
func (j *Integration) PrepRequestParamsAndAuth(req *http.Request) error {
return j.prepRequest(req)
}

// TODO migrate strings
// PrepRequestBody formats body data to meet the API requirements.
func (j *Integration) PrepRequestBody(body interface{}, method string, endpoint string) ([]byte, error) {
return j.marshalRequest(body, method, endpoint)
}

// TODO migrate strings
// TODO this comment
func (j *Integration) MarshalMultipartRequest(fields map[string]string, files map[string]string) ([]byte, string, error) {
return j.marshalMultipartRequest(fields, files)
}

// TODO migrate strings
// GetSessionCookies retrieves all cookies from the current session
func (j *Integration) GetSessionCookies() ([]*http.Cookie, error) {
domain := j.GetFQDN()
return j.getSessionCookies(domain)
Expand Down
6 changes: 3 additions & 3 deletions jamf/jamfprointegration/load_balancer_workaround.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"slices"
)

// TODO migrate strings
// GetSessionCookies retrieves all cookies from the current session
func (j *Integration) getSessionCookies(urlString string) ([]*http.Cookie, error) {
var returnList []*http.Cookie
balancerValue, err := j.GetLoadBalancer(urlString)
Expand All @@ -17,7 +17,7 @@ func (j *Integration) getSessionCookies(urlString string) ([]*http.Cookie, error
return returnList, nil
}

// TODO migrate strings
// GetLoadBalancer programatically always returns the most alphabetical load balancer from a session
func (j *Integration) GetLoadBalancer(urlString string) (string, error) {
allBalancers, err := j.getAllLoadBalancers(urlString)
if err != nil {
Expand All @@ -28,7 +28,7 @@ func (j *Integration) GetLoadBalancer(urlString string) (string, error) {
return chosenCookie, nil
}

// TODO migrate strings
// chooseMostAlphabeticalString returns the most alphabetical string from a list of strings
func chooseMostAlphabeticalString(strings []string) string {
if len(strings) == 0 {
return ""
Expand Down

0 comments on commit ab046a3

Please sign in to comment.