Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Cookie client for embed code into Gitlab site tree #1906

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const (
JobToken
OAuthToken
PrivateToken
GitlabCookie
)

var ErrNotFound = errors.New("404 Not Found")
Expand Down Expand Up @@ -91,6 +92,9 @@ type Client struct {
// Token type used to make authenticated API calls.
authType AuthType

// Auth Cookie for internal requests
cookie []*http.Cookie

// Username and password used for basic authentication.
username, password string

Expand Down Expand Up @@ -281,6 +285,18 @@ func NewBasicAuthClient(username, password string, options ...ClientOptionFunc)
return client, nil
}

func NewCookieClient(cookie []*http.Cookie, options ...ClientOptionFunc) (*Client, error) {
client, err := newClient(options...)
if err != nil {
return nil, err
}

client.authType = GitlabCookie
client.cookie = cookie

return client, nil
}

// NewJobClient returns a new GitLab API client. To use API methods which require
// authentication, provide a valid job token.
func NewJobClient(token string, options ...ClientOptionFunc) (*Client, error) {
Expand Down Expand Up @@ -863,6 +879,13 @@ func (c *Client) Do(req *retryablehttp.Request, v interface{}) (*Response, error
if values := req.Header.Values("PRIVATE-TOKEN"); len(values) == 0 {
req.Header.Set("PRIVATE-TOKEN", c.token)
}
case GitlabCookie:
sendCookie := c.cookie
if len(sendCookie) > 0 {
for _, value := range sendCookie {
req.Header.Add("Cookie", value.String())
}
}
}

resp, err := c.client.Do(req)
Expand Down