Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | ) |
| 6 | |
| 7 | // WithReauth wraps a Perigee request fragment with logic to perform re-authentication |
| 8 | // if it's deemed necessary. |
| 9 | // |
| 10 | // Do not confuse this function with WithReauth()! Although they work together to support reauthentication, |
| 11 | // WithReauth() actually contains the decision-making logic to determine when to perform a reauth, |
| 12 | // while WithReauthHandler() is used to configure what a reauth actually entails. |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 13 | func (c *Context) WithReauth(ap AccessProvider, f func() error) error { |
| 14 | err := f() |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 15 | cause, ok := err.(*perigee.UnexpectedResponseCodeError) |
| 16 | if ok && cause.Actual == 401 { |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 17 | err = c.reauthHandler(ap) |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 18 | if err == nil { |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 19 | err = f() |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 20 | } |
| 21 | } |
Samuel A. Falvo II | 2f50b14 | 2013-07-16 11:38:03 -0700 | [diff] [blame] | 22 | return err |
Samuel A. Falvo II | 1206f85 | 2013-07-15 17:56:51 -0700 | [diff] [blame] | 23 | } |