/* Clear will delete things. If toDelete is empty, EVERYTHING will be deleted. If toDelete has one element, that index will be deleted. If toDelete has two elements, that index and doc type will be deleted. */ func Clear(c ElasticConnector, toDelete ...string) (err error) { url := c.GetElasticService() if len(toDelete) > 2 { err = errors.Errorf("Can only give at most 2 string args to Clear") return } else if len(toDelete) == 2 { url += fmt.Sprintf("/%v/%v", processIndexName(toDelete[0]), toDelete[1]) } else if len(toDelete) == 1 { url += fmt.Sprintf("/%v", processIndexName(toDelete[0])) } else { url += "/_all" } request, err := http.NewRequest("DELETE", url, nil) if err != nil { return } if c.GetElasticUsername() != "" { request.SetBasicAuth(c.GetElasticUsername(), c.GetElasticPassword()) } response, err := c.Client().Do(request) if err != nil { return } defer response.Body.Close() if response.StatusCode != http.StatusOK { err = errors.Errorf("Bad status trying to delete from elasticsearch %v: %v", url, response.Status) return } return }
func ErrorfWithLogger(logger Logger, format string, a ...interface{}) *errors.Error { if logger.Name() == "machine" { logger.Fprintln(os.Stderr, constants.LOG_LEVEL_ERROR, format, a...) return errors.Errorf("") } return errors.Errorf(Format(format, a...)) }
/* AddToIndex adds source to a search index. Source must have a field `Id *datastore.key`. */ func AddToIndex(c ElasticConnector, index string, source interface{}) (err error) { sourceVal := reflect.ValueOf(source) if sourceVal.Kind() != reflect.Ptr { err = errors.Errorf("%#v is not a pointer", source) return } if sourceVal.Elem().Kind() != reflect.Struct { err = errors.Errorf("%#v is not a pointer to a struct", source) return } index = processIndexName(index) value := reflect.ValueOf(source).Elem() id := value.FieldByName("Id").Interface().(key.Key).Encode() name := value.Type().Name() json, err := json.Marshal(source) if err != nil { return } url := fmt.Sprintf("%s/%s/%s/%s", c.GetElasticService(), index, name, id) updatedAtField := value.FieldByName("UpdatedAt") if updatedAtField.IsValid() { updatedAtUnixNano := updatedAtField.MethodByName("UnixNano") if updatedAtUnixNano.IsValid() { if unixNano, ok := updatedAtUnixNano.Call(nil)[0].Interface().(int64); ok && unixNano > 0 { millis := unixNano / 1000000 url = fmt.Sprintf("%v?version_type=external_gte&version=%v", url, millis) } } } request, err := http.NewRequest("PUT", url, bytes.NewBuffer(json)) if err != nil { return } if c.GetElasticUsername() != "" { request.SetBasicAuth(c.GetElasticUsername(), c.GetElasticPassword()) } response, err := c.Client().Do(request) if err != nil { return } defer response.Body.Close() if response.StatusCode != http.StatusCreated && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusConflict { body, _ := ioutil.ReadAll(response.Body) err = errors.Errorf("Bad status code from elasticsearch %v: %v, %v", url, response.Status, string(body)) return } return }
/* GenerateFlags will generate command line flags matching the fields of the provided interface (being a struct pointer). Any fields tagged with `flag` will be named like the value of the `flag` tag. */ func GenerateFlags(i interface{}) (result []string, err error) { v := reflect.ValueOf(i) t := v.Type() if t.Kind() != reflect.Ptr { err = errors.Errorf("Unable to ParseFlags into %v, it is not a pointer to a struct", v) return } t = t.Elem() v = v.Elem() if t.Kind() != reflect.Struct { err = errors.Errorf("Unable to ParseFlags into %v, it is not a pointer to a struct", v) return } for i := 0; i < t.NumField(); i++ { f := t.Field(i) flagName := f.Name if explicitFlagName := f.Tag.Get("flag"); explicitFlagName != "" { flagName = explicitFlagName } result = append(result, fmt.Sprintf("-%v=%v", flagName, v.Field(i).Interface())) } return }
// Validate : Validates a token and returns its signature or an error if the token is not valid. func (j *JWTEnigma) Validate(token string) (string, error) { split := strings.Split(token, ".") if len(split) != 3 { return "", errors.New("Header, body and signature must all be set") } // Parse the token. parsedToken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) { if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok { return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"]) } return jwt.ParseRSAPublicKeyFromPEM(j.PublicKey) }) if err != nil { return "", errors.Errorf("Couldn't parse token: %v", err) } else if !parsedToken.Valid { return "", errors.Errorf("Token is invalid") } // make sure we can work with the data claimsContext := jwthelper.ClaimsContext(parsedToken.Claims) if claimsContext.AssertExpired() { parsedToken.Valid = false return "", errors.Errorf("Token expired at %v", claimsContext.GetExpiresAt()) } if claimsContext.AssertNotYetValid() { parsedToken.Valid = false return "", errors.Errorf("Token validates in the future: %v", claimsContext.GetNotBefore()) } return split[2], nil }
func (d *google) FetchSession(code string) (Session, error) { conf := *d.conf token, err := conf.Exchange(oauth2.NoContext, code) if err != nil { return nil, err } if !token.Valid() { return nil, errors.Errorf("Token is not valid: %v", token) } c := conf.Client(oauth2.NoContext, token) req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s", d.api, "plus/v1/people/me"), nil) resp, err := c.Do(req) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, errors.Errorf("Could not fetch account data because %s", err) } var profile map[string]interface{} if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil { return nil, errors.Errorf("Could not validate id token because %s", err) } return &DefaultSession{ RemoteSubject: fmt.Sprintf("%s", profile["id"]), Extra: profile, }, nil }
func isValidAuthenticationRequest(c *HTTPClient, token string, retry bool) (bool, error) { data := url.Values{} data.Set("token", token) request := gorequest.New() resp, body, errs := request.Post(pkg.JoinURL(c.ep, "/oauth2/introspect")).Type("form").SetBasicAuth(c.clientConfig.ClientID, c.clientConfig.ClientSecret).SendString(data.Encode()).End() if len(errs) > 0 { return false, errors.Errorf("Got errors: %v", errs) } else if resp.StatusCode != http.StatusOK { return false, errors.Errorf("Status code %d is not 200: %s", resp.StatusCode, body) } if retry && resp.StatusCode == http.StatusUnauthorized { var err error if c.clientToken, err = c.clientConfig.Token(oauth2.NoContext); err != nil { return false, errors.New(err) } else if c.clientToken == nil { return false, errors.New("Access token could not be retrieved") } return isValidAuthenticationRequest(c, token, false) } else if resp.StatusCode != http.StatusOK { return false, fmt.Errorf("Status code %d is not 200", resp.StatusCode) } var introspect struct { Active bool `json:"active"` } if err := json.Unmarshal([]byte(body), &introspect); err != nil { return false, err } else if !introspect.Active { return false, errors.New("Authentication denied") } return introspect.Active, nil }
func isValidAuthorizeRequest(c *HTTPClient, ar *AuthorizeRequest, retry bool) (bool, error) { request := gorequest.New() resp, body, errs := request.Post(pkg.JoinURL(c.ep, "/guard/allowed")).SetBasicAuth(c.clientConfig.ClientID, c.clientConfig.ClientSecret).Set("Content-Type", "application/json").Send(*ar).End() if len(errs) > 0 { return false, errors.Errorf("Got errors: %v", errs) } else if retry && resp.StatusCode == http.StatusUnauthorized { var err error if c.clientToken, err = c.clientConfig.Token(oauth2.NoContext); err != nil { return false, errors.New(err) } else if c.clientToken == nil { return false, errors.New("Access token could not be retrieved") } return isValidAuthorizeRequest(c, ar, false) } else if resp.StatusCode != http.StatusOK { return false, errors.Errorf("Status code %d is not 200: %s", resp.StatusCode, body) } if err := json.Unmarshal([]byte(body), &isAllowed); err != nil { return false, errors.Errorf("Could not unmarshall body because %s", err.Error()) } if !isAllowed.Allowed { return false, errors.New("Authroization denied") } return isAllowed.Allowed, nil }
func (d *dropbox) FetchSession(code string) (Session, error) { conf := *d.conf token, err := conf.Exchange(oauth2.NoContext, code) if err != nil { return nil, err } if !token.Valid() { return nil, errors.Errorf("Token is not valid: %v", token) } c := conf.Client(oauth2.NoContext, token) req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", d.api, "users/get_current_account"), nil) response, err := c.Do(req) if err != nil { return nil, err } defer response.Body.Close() if response.StatusCode != http.StatusOK { return nil, errors.Errorf("Could not fetch account data because %s", err) } var acc map[string]interface{} if err = json.NewDecoder(response.Body).Decode(&acc); err != nil { return nil, err } return &DefaultSession{ RemoteSubject: fmt.Sprintf("%s", acc["account_id"]), Extra: acc, }, nil }
// Verify a token and output the claims. func (j *JWT) VerifyToken(tokenData []byte) (*jwt.Token, error) { // trim possible whitespace from token tokenData = regexp.MustCompile(`\s*$`).ReplaceAll(tokenData, []byte{}) // Parse the token. Load the key from command line option token, err := jwt.Parse(string(tokenData), func(t *jwt.Token) (interface{}, error) { if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok { return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"]) } return jwt.ParseRSAPublicKeyFromPEM(j.publicKey) }) if err != nil { return nil, errors.Errorf("Couldn't parse token: %v", err) } else if !token.Valid { return nil, errors.Errorf("Token is invalid") } claims := ClaimsCarrier(token.Claims) if claims.AssertExpired() { token.Valid = false return token, errors.Errorf("Token expired: %v", claims.GetExpiresAt()) } if claims.AssertNotYetValid() { token.Valid = false return token, errors.Errorf("Token is not valid yet: %v", claims.GetNotBefore()) } return token, nil }
func (d *signin) FetchSession(code string) (Session, error) { request := gorequest.New() // FIXME does not work if redirect contains query params resp, body, errs := request.Get(fmt.Sprintf("%s?verify=%s", d.login, code)).End() if len(errs) > 0 { return nil, errors.Errorf("Could not exchange code: %s", errs) } else if resp.StatusCode != http.StatusOK { return nil, errors.Errorf("Could not exchange code, received status: %d", resp.StatusCode) } var p payload if err := json.Unmarshal([]byte(body), &p); err != nil { return nil, errors.Errorf("Could not parse answer: %v", err) } if p.Subject == "" { return nil, errors.Errorf("Field subject is empty, got %s", body) } return &DefaultSession{ ForceLocalSubject: p.Subject, Extra: map[string]interface{}{}, }, nil }
func (g *RS256Generator) Generate(id string) (*jose.JsonWebKeySet, error) { if g.KeyLength < 4096 { g.KeyLength = 4096 } key, err := rsa.GenerateKey(rand.Reader, g.KeyLength) if err != nil { return nil, errors.Errorf("Could not generate key because %s", err) } else if err = key.Validate(); err != nil { return nil, errors.Errorf("Validation failed because %s", err) } // jose does not support this... key.Precomputed = rsa.PrecomputedValues{} return &jose.JsonWebKeySet{ Keys: []jose.JsonWebKey{ { Key: key, KeyID: ider("private", id), Certificates: []*x509.Certificate{}, }, { Key: &key.PublicKey, KeyID: ider("public", id), Certificates: []*x509.Certificate{}, }, }, }, nil }
// Submit sends the provided envelope to stellar-core and parses the response into // a SubmissionResult func (sub *submitter) Submit(ctx context.Context, env string) (result SubmissionResult) { start := time.Now() defer func() { result.Duration = time.Since(start) }() // construct the request u, err := url.Parse(sub.coreURL) if err != nil { result.Err = errors.Wrap(err, 1) return } u.Path = "/tx" q := u.Query() q.Add("blob", env) u.RawQuery = q.Encode() req, err := http.NewRequest("GET", u.String(), nil) if err != nil { result.Err = errors.Wrap(err, 1) return } // perform the submission resp, err := sub.http.Do(req) if err != nil { result.Err = errors.Wrap(err, 1) return } defer resp.Body.Close() // parse response var cresp coreSubmissionResponse err = json.NewDecoder(resp.Body).Decode(&cresp) if err != nil { result.Err = errors.Wrap(err, 1) return } // interpet response if cresp.Exception != "" { result.Err = errors.Errorf("stellar-core exception: %s", cresp.Exception) return } switch cresp.Status { case StatusError: result.Err = &FailedTransactionError{cresp.Error} case StatusPending, StatusDuplicate: //noop. A nil Err indicates success default: result.Err = errors.Errorf("Unrecognized stellar-core status response: %s", cresp.Status) } return }
func DelAll(c PersistenceContext, src interface{}) (err error) { srcTyp := reflect.TypeOf(src) if srcTyp.Kind() != reflect.Ptr { err = errors.Errorf("%+v is not a pointer", src) return } if srcTyp.Elem().Kind() != reflect.Struct { err = errors.Errorf("%+v is not a pointer to a struct", src) return } return DelQuery(c, src, datastore.NewQuery(reflect.TypeOf(src).Elem().Name())) }
func (s *DefaultConsentStrategy) ValidateResponse(a fosite.AuthorizeRequester, token string) (claims *Session, err error) { t, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) { if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok { return nil, errors.Errorf("Unexpected signing method: %v", t.Header["alg"]) } pk, err := s.KeyManager.GetKey(ConsentEndpointKey, "public") if err != nil { return nil, err } rsaKey, ok := jwk.First(pk.Keys).Key.(*rsa.PublicKey) if !ok { return nil, errors.New("Could not convert to RSA Private Key") } return rsaKey, nil }) if err != nil { return nil, errors.Errorf("Couldn't parse token: %v", err) } else if !t.Valid { return nil, errors.Errorf("Token is invalid") } if time.Now().After(ejwt.ToTime(t.Claims["exp"])) { return nil, errors.Errorf("Token expired") } if ejwt.ToString(t.Claims["aud"]) != a.GetClient().GetID() { return nil, errors.Errorf("Audience mismatch") } subject := ejwt.ToString(t.Claims["sub"]) for _, scope := range toStringSlice(t.Claims["scp"]) { a.GrantScope(scope) } return &Session{ Subject: subject, DefaultSession: &strategy.DefaultSession{ Claims: &ejwt.IDTokenClaims{ Audience: a.GetClient().GetID(), Subject: subject, Issuer: s.Issuer, IssuedAt: time.Now(), ExpiresAt: time.Now(), Extra: t.Claims, }, Headers: &ejwt.Headers{}, }, }, err }
func checkManifest() (string, string, error) { manifest := os.Getenv("WERCKER_DISTELLI_MANIFEST") if manifest == "" { return "", "", errors.Errorf("manifest must be set") } if _, err := os.Stat(manifest); err != nil { return "", "", errors.Errorf("manifest file %s not found", manifest) } dirname, basename := path.Split(manifest) return dirname, basename, nil }
func (f *RrdRawFile) readVersionHeader(reader *RawDataReader) error { if cookie, err := reader.ReadCString(4); err != nil { return err } else if cookie != rrdCookie { return errors.Errorf("Invalid cookie: %+v", cookie) } versionStr, err := reader.ReadCString(5) if err != nil { return err } version, err := strconv.ParseInt(string(versionStr[:4]), 10, 16) if err != nil { return errors.Errorf("Invalid version: %+v", version) } else if version < 3 { return errors.Errorf("Version %d not supported: ", version) } if floatCookie, err := reader.ReadDouble(); err != nil { return err } else if floatCookie != rrdFloatCookie { return errors.Errorf("Float cookie does not match: %+v != %+v", floatCookie, rrdFloatCookie) } datasourceCount, err := reader.ReadUnsignedLong() if err != nil { return errors.Wrap(err, 0) } rraCount, err := reader.ReadUnsignedLong() if err != nil { return errors.Wrap(err, 0) } pdpStep, err := reader.ReadUnsignedLong() if err != nil { return errors.Wrap(err, 0) } if _, err = reader.ReadUnivals(10); err != nil { return errors.Wrap(err, 0) } f.header = &rrdRawHeader{ version: uint16(version), datasourceCount: datasourceCount, rraCount: rraCount, pdpStep: pdpStep, } return nil }
func (f *RrdRawFile) encodeRraCpdPreps(rraIndex, dsIndex int, rv reflect.Value) error { if rv.Kind() != reflect.Struct { return errors.Errorf("Cdp params must be a struct") } for i := 0; i < rv.Type().NumField(); i++ { field := rv.Type().Field(i) if field.Type.Kind() == reflect.Struct { if err := f.encodeRraCpdPreps(rraIndex, dsIndex, rv.Field(i)); err != nil { return err } continue } scratch := f.cdpPreps[rraIndex][dsIndex].scratch tag := field.Tag.Get("cdp") if tag == "" { continue } if tag == "raw" && field.Type.Kind() == reflect.Slice { switch field.Type.Elem().Kind() { case reflect.Uint8: raw := rv.Field(i).Interface().([]uint8) values := f.dataFile.BytesToUnivals(raw) for i, v := range values { scratch[i] = v } default: return errors.Errorf("cpd scatch must have type []uint64") } } else { scratchIndex, err := strconv.ParseInt(tag, 10, 64) if err != nil { return err } switch field.Type.Kind() { case reflect.Uint64: scratch[scratchIndex] = univalForUnsignedLong(rv.Field(i).Uint()) case reflect.Float64: scratch[scratchIndex] = univalForDouble(rv.Field(i).Float()) default: return errors.Errorf("cpd field must have type uint64 or float64") } } } return nil }
func PoliciesFromContext(ctx context.Context) ([]policy.Policy, error) { args, ok := ctx.Value(authKey).(*authorization) if !ok { return nil, errors.Errorf("Could not assert array type for %s", ctx.Value(authKey)) } symbols := make([]policy.Policy, len(args.policies)) for i, arg := range args.policies { symbols[i], ok = arg.(*policy.DefaultPolicy) if !ok { return nil, errors.Errorf("Could not assert policy type for %s", ctx.Value(authKey)) } } return symbols, nil }
func SubjectFromContext(ctx context.Context) (string, error) { args, ok := ctx.Value(authKey).(*authorization) if !ok { return "", errors.Errorf("Could not assert type for %v", ctx.Value(authKey)) } return args.claims.GetSubject(), nil }
func TokenFromContext(ctx context.Context) (*jwt.Token, error) { args, ok := ctx.Value(authKey).(*authorization) if !ok { return nil, errors.Errorf("Could not assert type for %v", ctx.Value(authKey)) } return args.token, nil }
func (d *DatasourceDerive) CalculatePdpPrep(newValue string, interval float64) (float64, error) { if float64(d.Heartbeat) < interval { d.LastValue = Undefined } rate := math.NaN() newPdp := math.NaN() if newValue != Undefined && float64(d.Heartbeat) >= interval { newInt := new(big.Int) _, err := fmt.Sscan(newValue, newInt) if err != nil { return math.NaN(), errors.Errorf("not a simple signed integer: %s", newValue) } if d.LastValue != "U" { prevInt := new(big.Int) _, err := fmt.Sscan(d.LastValue, prevInt) if err != nil { return math.NaN(), errors.Wrap(err, 0) } diff := new(big.Int) diff.Sub(newInt, prevInt) newPdp = float64(diff.Uint64()) rate = newPdp / interval } } if !d.checkRateBounds(rate) { newPdp = math.NaN() } d.LastValue = newValue return newPdp, nil }
func findToolUrl(index map[string]interface{}, tool Tool, host []string) (string, error) { if len(tool.OsUrls) > 0 { for _, osUrl := range tool.OsUrls { if utils.SliceContains(host, osUrl.Os) { return osUrl.Url, nil } } } else { packages := index["packages"].([]interface{}) for _, p := range packages { pack := p.(map[string]interface{}) packageTools := pack[constants.PACKAGE_TOOLS].([]interface{}) for _, pt := range packageTools { packageTool := pt.(map[string]interface{}) name := packageTool[constants.TOOL_NAME].(string) version := packageTool[constants.TOOL_VERSION].(string) if name == tool.Name && version == tool.Version { systems := packageTool["systems"].([]interface{}) for _, s := range systems { system := s.(map[string]interface{}) if utils.SliceContains(host, system["host"].(string)) { return system[constants.TOOL_URL].(string), nil } } } } } } return constants.EMPTY_STRING, errors.Errorf("Unable to find tool " + tool.Name + " " + tool.Version) }
// Open a CDataFile func OpenRawDataFile(name string, readOnly bool, byteOrder binary.ByteOrder, byteAlignment uint64, valueSize int) (*RawDataFile, error) { flag := os.O_RDWR if readOnly { flag = os.O_RDONLY } file, err := os.OpenFile(name, flag, 0644) if err != nil { return nil, err } var univalToBytes func([]byte, unival) var bytesToUnival func([]byte) unival switch valueSize { case 8: univalToBytes = func(dst []byte, src unival) { byteOrder.PutUint64(dst, src.AsUnsignedLong()) } bytesToUnival = func(src []byte) unival { return unival(byteOrder.Uint64(src)) } default: return nil, errors.Errorf("Invalid value size %d", valueSize) } return &RawDataFile{ file: file, // byteOrder: byteOrder, byteAlignment: byteAlignment, valueSize: valueSize, univalToBytes: univalToBytes, bytesToUnival: bytesToUnival, }, nil }
func (c *AEAD) Decrypt(ciphertext string) ([]byte, error) { if len(c.Key) < 32 { return []byte{}, errors.Errorf("Key must be longer 32 bytes, got %d bytes", len(c.Key)) } raw, err := base64.URLEncoding.DecodeString(ciphertext) if err != nil { return []byte{}, errors.New(err) } n := len(raw) block, err := aes.NewCipher(c.Key) if err != nil { return []byte{}, errors.New(err) } aesgcm, err := cipher.NewGCM(block) if err != nil { return []byte{}, errors.New(err) } plaintext, err := aesgcm.Open(nil, raw[n-12:n], raw[:n-12], nil) if err != nil { return []byte{}, errors.New(err) } return plaintext, nil }
func (c *AEAD) Encrypt(plaintext []byte) (string, error) { // The key argument should be the AES key, either 16 or 32 bytes // to select AES-128 or AES-256. if len(c.Key) < 32 { return "", errors.Errorf("Key must be longer 32 bytes, got %d bytes", len(c.Key)) } block, err := aes.NewCipher(c.Key[:32]) if err != nil { return "", errors.New(err) } nonce := make([]byte, 12) if _, err := io.ReadFull(rand.Reader, nonce); err != nil { return "", errors.New(err) } aesgcm, err := cipher.NewGCM(block) if err != nil { return "", errors.New(err) } ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil) return base64.URLEncoding.EncodeToString(append(ciphertext, nonce...)), nil }
func removeOldNotifications(s *State) { throttleRequests := make(chan bool, 20) defer close(throttleRequests) toDelete := []models.ParseNotification{} for _, n := range s.Notifications { // Deletes things from the day before gives a day leway... if n.OnDate.ISO.Before(time.Now().AddDate(0, 0, -1)) { toDelete = append(toDelete, n) } } for _, n := range toDelete { go func(n models.ParseNotification) { defer func() { <-throttleRequests }() status, errs := s.DB.Delete(parse.Params{ Class: "Notification", ObjectID: n.ObjectID(), }, nil) if status == 200 || status == 201 { fmt.Println("Successfully deleted notification with ID:", n.UUID) } if errs != nil { fmt.Print(status) fmt.Print(errors.Errorf("Unable to delete Notification with ID: %s", n.UUID)) } }(n) throttleRequests <- true } for i := 0; i < cap(throttleRequests); i++ { throttleRequests <- false } }
func (d *dropbox) Exchange(code string) (Session, error) { conf := *d.conf token, err := conf.Exchange(oauth2.NoContext, code) if err != nil { return nil, err } if !token.Valid() { return nil, errors.Errorf("Token is not valid: %v", token) } c := conf.Client(oauth2.NoContext, token) req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s", d.api, "users/get_current_account"), nil) response, err := c.Do(req) if err != nil { return nil, err } defer response.Body.Close() body, err := ioutil.ReadAll(response.Body) if err != nil { return nil, err } var acc Account if err = json.Unmarshal(body, &acc); err != nil { return nil, err } return &DefaultSession{ RemoteSubject: acc.ID, Extra: acc, Token: token, }, nil }
func (r *Rrd) Update(timestamp time.Time, values []string) error { if timestamp.Before(r.LastUpdate) { return errors.Errorf("illegal attempt to update using time %s when last update time is %s (minimum one second step)", timestamp.String(), r.LastUpdate.String()) } elapsed := r.calculateElapsedSteps(timestamp) newPdps, err := r.calculatePdpPreps(elapsed.Interval, values) if err != nil { return err } if elapsed.Steps == 0 { r.simpleUpdate(newPdps, elapsed.Interval) } else { pdpTemp := r.processAllPdp(newPdps, elapsed) rraStepCounts := r.updateAllCdpPreps(pdpTemp, elapsed) r.updateAberrantCdps(pdpTemp, elapsed) r.writeToRras(rraStepCounts) for i, rra := range r.Rras { if err := r.Store.StoreRraParams(i, rra); err != nil { return err } } } return r.writeChanges(timestamp) }
func (s *SuperAgent) Get(o interface{}) error { req, err := http.NewRequest("GET", s.URL, nil) if err != nil { return errors.New(err) } else if o == nil { return errors.New("Can not pass nil") } if err := s.doDry(req); err != nil { return err } resp, err := s.Client.Do(req) if err != nil { return errors.New(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { body, _ := ioutil.ReadAll(resp.Body) return errors.Errorf("Expected status code %d, got %d.\n%s\n", http.StatusOK, resp.StatusCode, body) } else if err := json.NewDecoder(resp.Body).Decode(o); err != nil { return errors.New(err) } return nil }