GetTokenViaBasic retrieves a token using only the password @results: accessToken, refreshToken, accessExpiry, refreshExpiry, pds, did, hash, salt, uuid, error
(username string, password string)
| 278 | // GetTokenViaBasic retrieves a token using only the password |
| 279 | // @results: accessToken, refreshToken, accessExpiry, refreshExpiry, pds, did, hash, salt, uuid, error |
| 280 | func GetTokenViaBasic(username string, password string) (*string, *string, *float64, *float64, *string, *string, *string, *string, *string, error) { |
| 281 | var tokens []Token |
| 282 | if err := db.Where("basic_auth_username = ?", username).Find(&tokens).Error; err != nil { |
| 283 | return nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("invalid credentials") |
| 284 | } |
| 285 | var token Token |
| 286 | if len(tokens) == 0 { |
| 287 | return nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("invalid credentials") |
| 288 | } |
| 289 | |
| 290 | for _, t := range tokens { |
| 291 | if err := bcrypt.CompareHashAndPassword([]byte(token.BasicAuthHash), []byte(password)); err != nil { |
| 292 | token = t |
| 293 | break |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if token.EncryptedAccessToken == "" { |
| 298 | return nil, nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("invalid credentials") |
| 299 | } |
| 300 | |
| 301 | encryptionKey := authcrypt.DeriveKeyFromPassword(password, token.BasicAuthSalt) |
| 302 | |
| 303 | accessToken, err := authcrypt.Decrypt(token.EncryptedAccessToken, encryptionKey) |
| 304 | if err != nil { |
| 305 | return nil, nil, nil, nil, nil, nil, nil, nil, nil, err |
| 306 | } |
| 307 | |
| 308 | refreshToken, err := authcrypt.Decrypt(token.EncryptedRefreshToken, encryptionKey) |
| 309 | if err != nil { |
| 310 | return nil, nil, nil, nil, nil, nil, nil, nil, nil, err |
| 311 | } |
| 312 | |
| 313 | return &accessToken, &refreshToken, &token.AccessExpiry, &token.RefreshExpiry, &token.UserPDS, &token.UserDid, &token.BasicAuthHash, &token.BasicAuthSalt, &token.TokenUUID, nil |
| 314 | } |
| 315 | |
| 316 | // DeleteToken deletes a token using did and uuid |
| 317 | func DeleteToken(did string, tokenUUID string) error { |