(ctx context.Context)
| 112 | } |
| 113 | |
| 114 | func (a *CliAuthenticator) LoadAuth(ctx context.Context) (*scalingo.User, *auth.UserToken, error) { |
| 115 | file, err := os.OpenFile(C.AuthFile, os.O_RDONLY, 0600) |
| 116 | if os.IsNotExist(err) { |
| 117 | return nil, nil, ErrUnauthenticated |
| 118 | } |
| 119 | if err != nil { |
| 120 | return nil, nil, errors.Wrapf(ctx, err, "open authentication file for read") |
| 121 | } |
| 122 | |
| 123 | var authConfig auth.ConfigData |
| 124 | if err := json.NewDecoder(file).Decode(&authConfig); err != nil { |
| 125 | file.Close() |
| 126 | return nil, nil, errors.Wrapf(ctx, err, "decode authentication file") |
| 127 | } |
| 128 | file.Close() |
| 129 | |
| 130 | if authConfig.AuthDataVersion != auth.ConfigVersionV2 && authConfig.AuthDataVersion != auth.ConfigVersionV21 { |
| 131 | err = writeAuthFile(ctx, &authConfig) |
| 132 | if err != nil { |
| 133 | return nil, nil, errors.Wrapf(ctx, err, "fail to update to authv2") |
| 134 | } |
| 135 | return nil, nil, ErrUnauthenticated |
| 136 | } |
| 137 | |
| 138 | var configPerHost auth.ConfigPerHostV2 |
| 139 | err = json.Unmarshal(authConfig.AuthConfigPerHost, &configPerHost) |
| 140 | if err != nil { |
| 141 | return nil, nil, errors.Wrapf(ctx, err, "unmarshal authentication config per host") |
| 142 | } |
| 143 | |
| 144 | if authConfig.AuthDataVersion == auth.ConfigVersionV2 { |
| 145 | authConfig.AuthDataVersion = auth.ConfigVersionV21 |
| 146 | configPerHost["auth.scalingo.com"] = configPerHost["api.scalingo.com"] |
| 147 | delete(configPerHost, "api.scalingo.com") |
| 148 | buffer, err := json.Marshal(&configPerHost) |
| 149 | if err != nil { |
| 150 | return nil, nil, errors.Wrapf(ctx, err, "migrate auth config v2.0 to v2.1") |
| 151 | } |
| 152 | authConfig.AuthConfigPerHost = json.RawMessage(buffer) |
| 153 | err = writeAuthFile(ctx, &authConfig) |
| 154 | if err != nil { |
| 155 | return nil, nil, errors.Wrapf(ctx, err, "migrate auth config v2.0 to v2.1") |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | authHost, err := a.authHost(ctx) |
| 160 | if err != nil { |
| 161 | return nil, nil, errors.Wrapf(ctx, err, "fail to get authentication service host") |
| 162 | } |
| 163 | |
| 164 | creds, ok := configPerHost[authHost] |
| 165 | if !ok || creds.User == nil { |
| 166 | return nil, nil, ErrUnauthenticated |
| 167 | } |
| 168 | return creds.User, creds.Tokens, nil |
| 169 | } |
| 170 | |
| 171 | func (a *CliAuthenticator) RemoveAuth(ctx context.Context) error { |
no test coverage detected