func SetAccessToken()

in internal/apiclient/token.go [226:262]


func SetAccessToken() error {
	if GetIntegrationToken() == "" && GetServiceAccount() == "" {
		SetIntegrationToken(getToken()) // read from configuration
		if GetIntegrationToken() == "" {
			return fmt.Errorf("either token or service account must be provided")
		}
		if checkAccessToken() { // check if the token is still valid
			return nil
		}
		return fmt.Errorf("token expired: request a new access token or pass the service account")
	}
	if GetIntegrationToken() != "" {
		// a token was passed, cache it
		if checkAccessToken() {
			_ = writeToken(GetIntegrationToken())
			return nil
		}
	} else {
		err := readServiceAccount(GetServiceAccount())
		if err != nil { // Handle errors reading the config file
			return fmt.Errorf("error reading config file: %s", err)
		}
		privateKey := getServiceAccountProperty("PrivateKey")
		if privateKey == "" {
			return fmt.Errorf("private key missing in the service account")
		}
		if getServiceAccountProperty("ClientEmail") == "" {
			return fmt.Errorf("client email missing in the service account")
		}
		_, err = generateAccessToken(privateKey)
		if err != nil {
			return fmt.Errorf("fatal error generating access token: %s", err)
		}
		return nil
	}
	return fmt.Errorf("token expired: request a new access token or pass the service account")
}