|
| 1 | +/* |
| 2 | +bitwarden implements an API client for bitwarden |
| 3 | +*/ |
| 4 | +package bitwarden |
| 5 | + |
| 6 | +import ( |
| 7 | + // Packages |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/mutablelogic/go-client/pkg/client" |
| 11 | + |
| 12 | + // Namespace imports |
| 13 | + . "github.com/djthorpe/go-errors" |
| 14 | +) |
| 15 | + |
| 16 | +/////////////////////////////////////////////////////////////////////////////// |
| 17 | +// TYPES |
| 18 | + |
| 19 | +type Client struct { |
| 20 | + *client.Client |
| 21 | +} |
| 22 | + |
| 23 | +type Session struct { |
| 24 | + Kdf struct { |
| 25 | + Iterations int `json:"kdfIterations"` |
| 26 | + } `json:"kdf"` |
| 27 | + Key []byte `json:"key"` |
| 28 | + Email string `json:"email"` |
| 29 | + Password string `json:"password"` |
| 30 | +} |
| 31 | + |
| 32 | +type reqPrelogin struct { |
| 33 | + Email string `json:"email"` |
| 34 | +} |
| 35 | + |
| 36 | +type reqToken struct { |
| 37 | + GrantType string `json:"grant_type"` |
| 38 | + Email string `json:"username"` |
| 39 | + Password string `json:"password"` |
| 40 | + Scope string `json:"scope"` |
| 41 | + ClientId string `json:"client_id"` |
| 42 | + DeviceType string `json:"deviceType"` |
| 43 | + DeviceName string `json:"deviceName"` |
| 44 | + DeviceIdentifier string `json:"deviceIdentifier"` |
| 45 | + DevicePushToken string `json:"devicePushToken"` |
| 46 | + TwoFactorToken string `json:"twoFactorToken,omitempty"` |
| 47 | + TwoFactorProvider int `json:"twoFactorProvider,omitempty"` |
| 48 | + TwoFactorRemember int `json:"twoFactorRemember,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +type respToken struct { |
| 52 | + AccessToken string `json:"access_token"` |
| 53 | + ExpiresIn int `json:"expires_in"` |
| 54 | + TokenType string `json:"token_type"` |
| 55 | + RefreshToken string `json:"refresh_token"` |
| 56 | + Key string `json:"key"` |
| 57 | +} |
| 58 | + |
| 59 | +/////////////////////////////////////////////////////////////////////////////// |
| 60 | +// GLOBALS |
| 61 | + |
| 62 | +const ( |
| 63 | + baseUrl = "https://api.bitwarden.com" |
| 64 | + identityUrl = "https://identity.bitwarden.com" |
| 65 | + iconUrl = "https://icons.bitwarden.net" |
| 66 | +) |
| 67 | + |
| 68 | +const ( |
| 69 | + deviceName = "api" |
| 70 | + loginScope = "api offline_access" |
| 71 | + loginApiKeyScope = "api" |
| 72 | +) |
| 73 | + |
| 74 | +/////////////////////////////////////////////////////////////////////////////// |
| 75 | +// LIFECYCLE |
| 76 | + |
| 77 | +func New(opts ...client.ClientOpt) (*Client, error) { |
| 78 | + // Create client |
| 79 | + client, err := client.New(append(opts, client.OptEndpoint(baseUrl))...) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + // Return the client |
| 85 | + return &Client{client}, nil |
| 86 | +} |
| 87 | + |
| 88 | +/////////////////////////////////////////////////////////////////////////////// |
| 89 | +// PUBLIC METHODS |
| 90 | + |
| 91 | +// Prelogin returns a session for logging in for a given email and password |
| 92 | +func (c *Client) Prelogin(email, password string) (*Session, error) { |
| 93 | + var request reqPrelogin |
| 94 | + var response Session |
| 95 | + |
| 96 | + // Prelogin |
| 97 | + request.Email = email |
| 98 | + payload, err := client.NewJSONRequest(request, client.ContentTypeJson) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } else if err := c.Do(payload.Post(), &response.Kdf, client.OptPath("accounts/prelogin")); err != nil { |
| 102 | + return nil, err |
| 103 | + } else if response.Kdf.Iterations == 0 { |
| 104 | + return nil, ErrUnexpectedResponse |
| 105 | + } |
| 106 | + |
| 107 | + // Create keys for encryption and decryption |
| 108 | + response.Email = email |
| 109 | + response.Key = MakeInternalKey(email, password, response.Kdf.Iterations) |
| 110 | + response.Password = HashedPassword(email, password, response.Kdf.Iterations) |
| 111 | + |
| 112 | + // Return success |
| 113 | + return &response, nil |
| 114 | +} |
| 115 | + |
| 116 | +// GetToken returns a token for a session |
| 117 | +func (c *Client) LoginToken(session *Session) error { |
| 118 | + var request reqToken |
| 119 | + |
| 120 | + request.GrantType = "password" |
| 121 | + request.Email = session.Email |
| 122 | + request.Password = session.Password |
| 123 | + request.Scope = loginScope |
| 124 | + request.ClientId = "browser" |
| 125 | + request.DeviceType = deviceType() |
| 126 | + request.DeviceName = deviceName |
| 127 | + request.DeviceIdentifier = "00000000-0000-0000-0000-000000000000" // TODO |
| 128 | +} |
| 129 | + |
| 130 | +/////////////////////////////////////////////////////////////////////////////// |
| 131 | +// PRIVATE METHODS |
| 132 | + |
| 133 | +func deviceType() string { |
| 134 | + switch runtime.GOOS { |
| 135 | + case "linux": |
| 136 | + return "8" // Linux Desktop |
| 137 | + case "darwin": |
| 138 | + return "7" // MacOS Desktop |
| 139 | + case "windows": |
| 140 | + return "6" // Windows Desktop |
| 141 | + default: |
| 142 | + return "14" // Unknown Browser |
| 143 | + } |
| 144 | +} |
0 commit comments