func GetTotpCode(totpUrl string) (*TotpCode, error) {
	// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
	// ex. otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30
	u, err := url.Parse(totpUrl)
	if err != nil || strings.ToLower(u.Scheme) != "otpauth" {
		return nil, errors.New("invalid TOTP URL: " + totpUrl)
	}
	m, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		return nil, errors.New("invalid TOTP URL query values: " + u.RawQuery)
	}

	secret := ""
	digits := 6
	algorithm := "SHA1"
	period := int64(30)

	// the only required parameter is the secret
	if value, ok := m["secret"]; ok {
		secret = strings.TrimSpace(value[0])
	}

	// fallback to defaults for optional parameters
	if value, ok := m["digits"]; ok {
		if d, err := strconv.Atoi(value[0]); err == nil {
			digits = d
		}
	}
	if value, ok := m["algorithm"]; ok && strings.TrimSpace(value[0]) != "" {
		algorithm = strings.TrimSpace(value[0])
	}
	if value, ok := m["period"]; ok {
		if d, err := strconv.Atoi(value[0]); err == nil {
			period = int64(d)
		}
	}

	totp := TOTP{
		Secret:    secret,
		Digits:    digits,
		Algorithm: algorithm,
		Period:    period,
	}

	if code, ttl, err := totp.Generate(); err == nil {
		return &TotpCode{
			Code:     code,
			TimeLeft: ttl,
			Period:   int(period),
		}, nil
	} else {
		return nil, err
	}
}
// Generates TOTP code from the URL and returns OTP as string, seconds remaining and any error encountered.
Generates TOTP code from the URL and returns OTP as string, seconds remaining and any error encountered.
// Generates TOTP code from the URL and returns OTP as string seconds remaining and any error encountered .
func GetTotpCode(totpUrl string) (*TotpCode, error) {
	u, err := url.Parse(totpUrl)
	if err != nil || strings.ToLower(u.Scheme) != "otpauth" {
		return nil, errors.New("invalid TOTP URL: " + totpUrl)
	}
	m, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		return nil, errors.New("invalid TOTP URL query values: " + u.RawQuery)
	}
	secret := ""
	digits := 6
	algorithm := "SHA1"
	period := int64(30)
	if value, ok := m["secret"]; ok {
		secret = strings.TrimSpace(value[0])
	}
	if value, ok := m["digits"]; ok {
		if d, err := strconv.Atoi(value[0]); err == nil {
			digits = d
		}
	}
	if value, ok := m["algorithm"]; ok && strings.TrimSpace(value[0]) != "" {
		algorithm = strings.TrimSpace(value[0])
	}
	if value, ok := m["period"]; ok {
		if d, err := strconv.Atoi(value[0]); err == nil {
			period = int64(d)
		}
	}
	totp := TOTP{
		Secret:    secret,
		Digits:    digits,
		Algorithm: algorithm,
		Period:    period,
	}
	if code, ttl, err := totp.Generate(); err == nil {
		return &TotpCode{
			Code:     code,
			TimeLeft: ttl,
			Period:   int(period),
		}, nil
	} else {
		return nil, err
	}
}
func GetTotpCode ( totpUrl string ) ( * TotpCode , error ) { u , err := url . Parse ( totpUrl ) if err != nil || strings . ToLower ( u . Scheme ) != " " { return nil , errors . New ( " " + totpUrl ) } m , err := url . ParseQuery ( u . RawQuery ) if err != nil { return nil , errors . New ( " " + u . RawQuery ) } secret := " " digits := 6 algorithm := " " period := int64 ( 30 ) if value , ok := m [ " " ] ; ok { secret = strings . TrimSpace ( value [ 0 ] ) } if value , ok := m [ " " ] ; ok { if d , err := strconv . Atoi ( value [ 0 ] ) ; err == nil { digits = d } } if value , ok := m [ " " ] ; ok && strings . TrimSpace ( value [ 0 ] ) != " " { algorithm = strings . TrimSpace ( value [ 0 ] ) } if value , ok := m [ " " ] ; ok { if d , err := strconv . Atoi ( value [ 0 ] ) ; err == nil { period = int64 ( d ) } } totp := TOTP { Secret : secret , Digits : digits , Algorithm : algorithm , Period : period , } if code , ttl , err := totp . Generate ( ) ; err == nil { return & TotpCode { Code : code , TimeLeft : ttl , Period : int ( period ) , } , nil } else { return nil , err } }
Generates TOTP code from the URL and returns OTP as string, seconds remaining and any error encountered.
Generates TOTP code from the URL and returns OTP as string seconds remaining and any error encountered .
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format // ex. otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 // the only required parameter is the secret // fallback to defaults for optional parameters