Compare commits
6 Commits
04e7d3e0f4
...
v1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 4978d84196 | |||
|
07fade16d3
|
|||
|
666369714b
|
|||
| 122d00c8f9 | |||
|
ef10711b6e
|
|||
|
25729ce492
|
20
README.md
20
README.md
@@ -26,15 +26,16 @@ go build -o canvasarchiver ./cmd/canvasarchiver
|
||||
./canvasarchiver -fo
|
||||
```
|
||||
|
||||
Or for videos-only mode (all Panopto videos flat, no files):
|
||||
```bash
|
||||
./canvasarchiver -vo
|
||||
```
|
||||
Or for videos-only mode (all Panopto videos flat, no files):
|
||||
```bash
|
||||
./canvasarchiver -vo
|
||||
```
|
||||
|
||||
2. On first run, you'll be prompted to authenticate:
|
||||
- Visit the provided OAuth URL
|
||||
- Authorize the application
|
||||
- Copy the authorization code back to the terminal
|
||||
2. On first run, you'll be prompted to choose an authentication method:
|
||||
- **[1] Login via browser (OAuth)**: Visit the provided URL, authorize, and paste the code
|
||||
- **[2] Use Canvas API token**: Enter a manually generated API token (e.g. `12230~...`)
|
||||
|
||||
The choice is saved and reused on subsequent runs.
|
||||
|
||||
3. Enter your Course ID when prompted (or use `-me` to download all enrolled courses)
|
||||
|
||||
@@ -54,7 +55,6 @@ go build -o canvasarchiver ./cmd/canvasarchiver
|
||||
| `-me` | Download all enrolled courses |
|
||||
| `-n` | Prefix modules with order numbers `[1]`, `[2]`, etc. |
|
||||
|
||||
|
||||
## Configuration
|
||||
|
||||
The following constants can be modified in `internal/config/config.go`:
|
||||
@@ -66,7 +66,7 @@ The following constants can be modified in `internal/config/config.go`:
|
||||
|
||||
## Authentication
|
||||
|
||||
Credentials are stored in `credentials.json` after the first successful login. The refresh token is automatically used for subsequent runs.
|
||||
Credentials are stored in `credentials.json` after the first successful authentication. The chosen method is remembered for subsequent runs — OAuth refresh tokens are automatically refreshed, and API tokens are validated before saving.
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ func main() {
|
||||
httpClient := &http.Client{}
|
||||
|
||||
authenticator := auth.NewAuthenticator(httpClient)
|
||||
accessToken, err := authenticator.GetAccessToken()
|
||||
accessToken, err := authenticator.GetToken()
|
||||
if err != nil {
|
||||
fmt.Printf("Authentication failed: %v\n", err)
|
||||
return
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.directme.in/Joren/CanvasArchiver/internal/config"
|
||||
"git.directme.in/Joren/CanvasArchiver/internal/models"
|
||||
@@ -21,31 +27,38 @@ func NewAuthenticator(client *http.Client) *Authenticator {
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Authenticator) GetAccessToken() (string, error) {
|
||||
func (a *Authenticator) GetToken() (string, error) {
|
||||
creds, err := LoadCredentials()
|
||||
if err == nil && creds.AuthMethod != "" {
|
||||
switch creds.AuthMethod {
|
||||
case "api":
|
||||
token := strings.TrimSpace(creds.APIToken)
|
||||
if token == "" {
|
||||
return a.promptMethod()
|
||||
}
|
||||
fmt.Print("[*] Validating saved API token... ")
|
||||
if err := a.validateAPIToken(token); err != nil {
|
||||
fmt.Println("FAILED")
|
||||
fmt.Printf("[!] %v\n", err)
|
||||
SaveCredentials(&models.Credentials{})
|
||||
return a.promptMethod()
|
||||
}
|
||||
fmt.Println("OK")
|
||||
return token, nil
|
||||
case "oauth":
|
||||
return a.refreshOrLogin()
|
||||
}
|
||||
}
|
||||
return a.promptMethod()
|
||||
}
|
||||
|
||||
func (a *Authenticator) refreshOrLogin() (string, error) {
|
||||
creds, err := LoadCredentials()
|
||||
if err != nil {
|
||||
|
||||
fmt.Println("--- Initial Canvas Login Required ---")
|
||||
fmt.Printf("Visit: %s/login/oauth2/auth?client_id=%s&response_type=code&redirect_uri=%s\n",
|
||||
config.BaseURL, config.ClientID, url.QueryEscape(config.RedirectURI))
|
||||
fmt.Print("Enter Code: ")
|
||||
var code string
|
||||
fmt.Scanln(&code)
|
||||
|
||||
tr, err := a.doTokenRequest(url.Values{
|
||||
"grant_type": {"authorization_code"},
|
||||
"client_id": {config.ClientID},
|
||||
"client_secret": {config.ClientSecret},
|
||||
"redirect_uri": {config.RedirectURI},
|
||||
"code": {code},
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
SaveCredentials(&models.Credentials{RefreshToken: tr.RefreshToken})
|
||||
fmt.Println("[+] Login successful.")
|
||||
return tr.AccessToken, nil
|
||||
return a.login()
|
||||
}
|
||||
if strings.TrimSpace(creds.RefreshToken) == "" {
|
||||
return a.login()
|
||||
}
|
||||
|
||||
fmt.Println("[*] Reusing saved refresh token...")
|
||||
@@ -54,11 +67,111 @@ func (a *Authenticator) GetAccessToken() (string, error) {
|
||||
"client_id": {config.ClientID},
|
||||
"client_secret": {config.ClientSecret},
|
||||
"refresh_token": {creds.RefreshToken},
|
||||
"redirect_uri": {config.RedirectURI},
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("[!] Refresh failed: %v\n", err)
|
||||
return a.login()
|
||||
}
|
||||
fmt.Println("[+] Session refreshed.")
|
||||
return tr.AccessToken, nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) promptMethod() (string, error) {
|
||||
fmt.Println("Select authentication method:")
|
||||
fmt.Println(" [1] Login via browser (OAuth)")
|
||||
fmt.Println(" [2] Use Canvas API token")
|
||||
fmt.Print("Choice (1/2): ")
|
||||
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
switch strings.TrimSpace(input) {
|
||||
case "2":
|
||||
return a.setupAPIToken()
|
||||
default:
|
||||
return a.login()
|
||||
}
|
||||
}
|
||||
|
||||
func (a *Authenticator) setupAPIToken() (string, error) {
|
||||
fmt.Print("Enter Canvas API token: ")
|
||||
token, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" {
|
||||
return "", fmt.Errorf("empty API token")
|
||||
}
|
||||
|
||||
fmt.Print("[*] Validating API token... ")
|
||||
if err := a.validateAPIToken(token); err != nil {
|
||||
fmt.Println("FAILED")
|
||||
return "", err
|
||||
}
|
||||
fmt.Println("OK")
|
||||
|
||||
if err := SaveCredentials(&models.Credentials{
|
||||
AuthMethod: "api",
|
||||
APIToken: token,
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Println("[+] API token saved.")
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) validateAPIToken(token string) error {
|
||||
req, _ := http.NewRequest("GET", config.BaseURL+"/api/v1/users/self", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := a.HTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 200 {
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("invalid token: %d %s", resp.StatusCode, strings.TrimSpace(string(body)))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) login() (string, error) {
|
||||
codeVerifier, codeChallenge, err := generatePKCEPair()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
fmt.Println("--- Initial Canvas Login Required ---")
|
||||
fmt.Printf("Visit: %s\n", buildAuthURL(codeChallenge))
|
||||
fmt.Print("Enter Code or redirect URL: ")
|
||||
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
if err != nil && err != io.EOF {
|
||||
return "", err
|
||||
}
|
||||
|
||||
code, err := extractCode(input)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
tr, err := a.doTokenRequest(url.Values{
|
||||
"grant_type": {"authorization_code"},
|
||||
"client_id": {config.ClientID},
|
||||
"client_secret": {config.ClientSecret},
|
||||
"redirect_uri": {config.RedirectURI},
|
||||
"code": {code},
|
||||
"code_verifier": {codeVerifier},
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Println("[+] Session refreshed.")
|
||||
|
||||
if err := SaveRefreshToken(tr.RefreshToken); err != nil {
|
||||
return "", err
|
||||
}
|
||||
fmt.Println("[+] Login successful.")
|
||||
return tr.AccessToken, nil
|
||||
}
|
||||
|
||||
@@ -70,17 +183,91 @@ func (a *Authenticator) doTokenRequest(v url.Values) (*models.TokenResponse, err
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("token request failed: %d", resp.StatusCode)
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("token request failed: %d: %s", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
var tr models.TokenResponse
|
||||
json.NewDecoder(resp.Body).Decode(&tr)
|
||||
if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tr, nil
|
||||
}
|
||||
|
||||
func SaveCredentials(creds *models.Credentials) {
|
||||
data, _ := json.MarshalIndent(creds, "", " ")
|
||||
os.WriteFile(config.CredsFile, data, 0o644)
|
||||
func buildAuthURL(codeChallenge string) string {
|
||||
u, _ := url.Parse(config.BaseURL + "/login/oauth2/auth")
|
||||
q := u.Query()
|
||||
q.Set("client_id", config.ClientID)
|
||||
q.Set("response_type", "code")
|
||||
q.Set("mobile", "1")
|
||||
q.Set("purpose", config.OAuthPurpose)
|
||||
q.Set("code_challenge", codeChallenge)
|
||||
q.Set("code_challenge_method", "S256")
|
||||
q.Set("redirect_uri", config.RedirectURI)
|
||||
if config.AuthProvider != "" {
|
||||
q.Set("authentication_provider", config.AuthProvider)
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func generatePKCEPair() (string, string, error) {
|
||||
randomBytes := make([]byte, 64)
|
||||
if _, err := rand.Read(randomBytes); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
verifier := base64.RawURLEncoding.EncodeToString(randomBytes)
|
||||
challengeBytes := sha256.Sum256([]byte(verifier))
|
||||
challenge := base64.RawURLEncoding.EncodeToString(challengeBytes[:])
|
||||
return verifier, challenge, nil
|
||||
}
|
||||
|
||||
func extractCode(input string) (string, error) {
|
||||
input = strings.TrimSpace(input)
|
||||
if input == "" {
|
||||
return "", fmt.Errorf("empty authorization code")
|
||||
}
|
||||
|
||||
for _, field := range strings.Fields(input) {
|
||||
if strings.Contains(field, "code=") {
|
||||
input = field
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
parsedURL, err := url.Parse(input)
|
||||
if err == nil {
|
||||
if code := parsedURL.Query().Get("code"); code != "" {
|
||||
return code, nil
|
||||
}
|
||||
}
|
||||
|
||||
if values, err := url.ParseQuery(input); err == nil {
|
||||
if code := values.Get("code"); code != "" {
|
||||
return code, nil
|
||||
}
|
||||
}
|
||||
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func SaveCredentials(creds *models.Credentials) error {
|
||||
data, err := json.MarshalIndent(creds, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(config.CredsFile, data, 0o644)
|
||||
}
|
||||
|
||||
func SaveRefreshToken(refreshToken string) error {
|
||||
creds, err := LoadCredentials()
|
||||
if err != nil {
|
||||
creds = &models.Credentials{}
|
||||
}
|
||||
creds.RefreshToken = refreshToken
|
||||
creds.AuthMethod = "oauth"
|
||||
return SaveCredentials(creds)
|
||||
}
|
||||
|
||||
func LoadCredentials() (*models.Credentials, error) {
|
||||
@@ -89,6 +276,8 @@ func LoadCredentials() (*models.Credentials, error) {
|
||||
return nil, err
|
||||
}
|
||||
var creds models.Credentials
|
||||
json.Unmarshal(data, &creds)
|
||||
if err := json.Unmarshal(data, &creds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &creds, nil
|
||||
}
|
||||
|
||||
@@ -16,6 +16,13 @@ import (
|
||||
"git.directme.in/Joren/CanvasArchiver/internal/utils"
|
||||
)
|
||||
|
||||
func checkAPIResponse(resp *http.Response) error {
|
||||
if resp.StatusCode < 300 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("API error: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
HTTPClient *http.Client
|
||||
AccessToken string
|
||||
@@ -48,6 +55,10 @@ func (c *Client) GetCourseInfo() error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkAPIResponse(resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var course models.Course
|
||||
json.NewDecoder(resp.Body).Decode(&course)
|
||||
|
||||
@@ -64,6 +75,10 @@ func (c *Client) GetEnrolledCourses() ([]models.Course, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkAPIResponse(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var courses []models.Course
|
||||
json.NewDecoder(resp.Body).Decode(&courses)
|
||||
return courses, nil
|
||||
|
||||
@@ -4,7 +4,9 @@ const (
|
||||
BaseURL = "https://canvas.vub.be"
|
||||
ClientID = "170000000000044"
|
||||
ClientSecret = "3sxR3NtgXRfT9KdpWGAFQygq6O9RzLN021h2lAzhHUZEeSQ5XGV41Ddi5iutwW6f"
|
||||
RedirectURI = "urn:ietf:wg:oauth:2.0:oob"
|
||||
RedirectURI = "https://sso.canvaslms.com/canvas/login"
|
||||
OAuthPurpose = "CanvasArchiver"
|
||||
AuthProvider = "microsoft"
|
||||
CredsFile = "credentials.json"
|
||||
PanoptoID = "15"
|
||||
UserAgent = "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Mobile Safari/537.36"
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package models
|
||||
|
||||
type Credentials struct {
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
AuthMethod string `json:"auth_method,omitempty"`
|
||||
RefreshToken string `json:"refresh_token,omitempty"`
|
||||
APIToken string `json:"api_token,omitempty"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user