Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 3c080d2

Browse files
committed
Finish authentication module
1 parent 5f01442 commit 3c080d2

File tree

4 files changed

+171
-98
lines changed

4 files changed

+171
-98
lines changed

authentication.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,54 @@ import (
88
type AuthenticationToken struct {
99
Success bool
1010
RequestToken string `json:"request_token"`
11-
ExpiresAt string `json:"expires_at"`
11+
ExpiresAt string `json:"expires_at,omitempty"`
1212
}
1313

14-
// GetNewToken generates a valid request token for user based authentication
14+
// AuthenticationSession struct
15+
type AuthenticationSession struct {
16+
Success bool
17+
SessionID string `json:"session_id"`
18+
}
19+
20+
// AuthenticationGuestSession struct
21+
type AuthenticationGuestSession struct {
22+
Success bool
23+
GuestSessionID string `json:"guest_session_id"`
24+
ExpiresAt string `json:"expires_at"`
25+
}
26+
27+
// GetAuthToken generates a valid request token for user based authentication
1528
// http://docs.themoviedb.apiary.io/#reference/authentication/authenticationtokennew/get
16-
func (tmdb *TMDb) GetNewToken() (*AuthenticationToken, error) {
29+
func (tmdb *TMDb) GetAuthToken() (*AuthenticationToken, error) {
1730
var token AuthenticationToken
1831
uri := fmt.Sprintf("%s/authentication/token/new?api_key=%s", baseURL, tmdb.apiKey)
1932
result, err := getTmdb(uri, &token)
2033
return result.(*AuthenticationToken), err
2134
}
35+
36+
// GetAuthValidateToken authenticates a user with a TMDb username and password
37+
// http://docs.themoviedb.apiary.io/#reference/authentication/authenticationtokenvalidatewithlogin/get
38+
func (tmdb *TMDb) GetAuthValidateToken(token, user, password string) (*AuthenticationToken, error) {
39+
var validToken AuthenticationToken
40+
uri := fmt.Sprintf("%s/authentication/token/validate_with_login?api_key=%s&request_token=%s&username=%s&password=%s", baseURL, tmdb.apiKey, token, user, password)
41+
result, err := getTmdb(uri, &validToken)
42+
return result.(*AuthenticationToken), err
43+
}
44+
45+
// GetAuthSession generates a session id for user based authentication
46+
// http://docs.themoviedb.apiary.io/#reference/authentication/authenticationsessionnew/get
47+
func (tmdb *TMDb) GetAuthSession(token string) (*AuthenticationSession, error) {
48+
var session AuthenticationSession
49+
uri := fmt.Sprintf("%s/authentication/session/new?api_key=%s&request_token=%s", baseURL, tmdb.apiKey, token)
50+
result, err := getTmdb(uri, &session)
51+
return result.(*AuthenticationSession), err
52+
}
53+
54+
// GetAuthGuestSession generates a valid request token for user based authentication
55+
// http://docs.themoviedb.apiary.io/#reference/authentication/authenticationguestsessionnew/get
56+
func (tmdb *TMDb) GetAuthGuestSession() (*AuthenticationGuestSession, error) {
57+
var session AuthenticationGuestSession
58+
uri := fmt.Sprintf("%s/authentication/guest_session/new?api_key=%s", baseURL, tmdb.apiKey)
59+
result, err := getTmdb(uri, &session)
60+
return result.(*AuthenticationGuestSession), err
61+
}

authentication_test.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@ import (
44
. "gopkg.in/check.v1"
55
)
66

7-
func (s *TmdbSuite) TestGetNewToken(c *C) {
8-
result, err := s.tmdb.GetNewToken()
7+
const (
8+
userToken string = ""
9+
user string = ""
10+
pw string = ""
11+
session string = ""
12+
guestSession string = ""
13+
)
14+
15+
func (s *TmdbSuite) TestGetAuthToken(c *C) {
16+
result, err := s.tmdb.GetAuthToken()
17+
s.baseTest(&result, err, c)
18+
}
19+
20+
func (s *TmdbSuite) TestGetAuthValidateToken(c *C) {
21+
result, err := s.tmdb.GetAuthValidateToken(userToken, user, pw)
22+
s.baseTest(&result, err, c)
23+
}
24+
25+
func (s *TmdbSuite) TestGetAuthSession(c *C) {
26+
result, err := s.tmdb.GetAuthSession(userToken)
27+
s.baseTest(&result, err, c)
28+
}
29+
30+
func (s *TmdbSuite) TestGetAuthGuestSession(c *C) {
31+
result, err := s.tmdb.GetAuthGuestSession()
932
s.baseTest(&result, err, c)
10-
c.Assert(result.Success, Equals, true)
11-
c.Assert(result.RequestToken, HasLen, 40)
1233
}

0 commit comments

Comments
 (0)